Geocoding
Contents
Geocoding is provided by @trimblemaps/services-react-native. Install it alongside @trimblemaps/account-react-native and ensure the account is initialized before calling geocoding functions. See Getting started — Packages for pinned package versions.
Forward geocoding
Convert an address or place name to coordinates:
import { geocode } from "@trimblemaps/services-react-native";
import { Camera, Map, TrimbleMaps } from "@trimblemaps/maps-react-native";
const response = await geocode("1 Independence Way Princeton NJ 08540");
const location = response.locations?.[0];
if (location?.coords) {
const { lat, lon } = location.coords;
cameraRef.current?.flyTo({
center: [lon, lat],
zoom: 13,
duration: 500,
});
}
The locations array contains TrimbleMapsLocation objects. Coordinates are in coords (lat, lon). The address field is an Address object (street, city, state, zip, and so on) — not a display string. Use placeName when present, or format address fields for UI:
import type { TrimbleMapsLocation } from "@trimblemaps/services-react-native";
function formatGeocodingLocation(
location: TrimbleMapsLocation | undefined,
): string {
if (!location) {
return "No results";
}
if (location.placeName?.trim()) {
return location.placeName;
}
const address = location.address;
if (address) {
const street = [address.streetNumber, address.street]
.filter(Boolean)
.join(" ")
.trim();
const locality = [address.city, address.state, address.zip]
.filter(Boolean)
.join(" ")
.trim();
const formatted = [street, locality].filter(Boolean).join(", ");
if (formatted) {
return formatted;
}
}
if (location.coords) {
return `${location.coords.lat.toFixed(5)}, ${location.coords.lon.toFixed(5)}`;
}
return "No address found";
}
Reverse geocoding
Convert coordinates to the nearest address:
import { reverseGeocode } from "@trimblemaps/services-react-native";
const LAT = 40.3573;
const LON = -74.6672;
const response = await reverseGeocode(LAT, LON, { maxResults: 1 });
const location = response.locations?.[0];
const label = formatGeocodingLocation(location);
On iOS, if the dedicated reverse geocode path returns empty results, you can fall back to coordinate-string forward geocoding:
import { Platform } from "react-native";
import { geocode, reverseGeocode } from "@trimblemaps/services-react-native";
async function fetchReverseGeocode(lat: number, lon: number) {
if (Platform.OS === "ios") {
return geocode(`${lat}, ${lon}`, { maxResults: 1 });
}
return reverseGeocode(lat, lon, { maxResults: 1 });
}
Wait for the map to finish loading (onDidFinishLoadingMap) before calling geocoding functions so the camera is ready to animate to results.