The Mapbox Geocoding API provides two main functions:
- Forward Geocoding: Convert addresses or place names into geographic coordinates (lat/lng)
- Reverse Geocoding: Convert geographic coordinates into human-readable addresses
For complete API reference and details, see the official Mapbox Geocoding API documentation.
Forward geocoding converts a text query (like "San Francisco" or "1600 Pennsylvania Avenue") into coordinates.
using UnityEngine;
using Mapbox.Unity.Map;
using Mapbox.GeocodingApi;
public class ForwardGeocodeExample : MonoBehaviour
{
public MapBehaviourCore MapCore;
private MapboxGeocodingApi _geocodingApi;
void Start()
{
MapCore.Initialized += map =>
{
_geocodingApi = new MapboxGeocodingApi(map.MapService.FileSource);
SearchPlace("San Francisco, CA");
};
}
void SearchPlace(string placeName)
{
var forwardGeocodeResource = new ForwardGeocodeResource(placeName);
_geocodingApi.Geocode(forwardGeocodeResource, (ForwardGeocodeResponse response) =>
{
if (response == null || response.Features == null || response.Features.Count == 0)
{
Debug.Log("No results found");
return;
}
var firstResult = response.Features[0];
Debug.Log($"Found: {firstResult.PlaceName}");
Debug.Log($"Coordinates: {firstResult.Center.x}, {firstResult.Center.y}");
});
}
}- Waits for the map to initialize
- Creates a geocoding request for "San Francisco, CA"
- Returns the place name and coordinates (latitude, longitude)
- The
Centerproperty contains the location as aVector2dwith x=latitude, y=longitude
You can refine your forward geocoding searches:
var forwardGeocodeResource = new ForwardGeocodeResource("pizza")
{
Country = new[] { "us" }, // Limit to United States
Proximity = new Vector2d(37.7, -122.4), // Bias results near San Francisco
Autocomplete = true, // Enable autocomplete suggestions
Types = new[] { "poi" } // Only return points of interest
};The ForwardGeocodeResponse contains:
Features- List of matching places (usually ordered by relevance)Query- The original query stringAttribution- Mapbox attribution text
Each Feature includes:
PlaceName- Full address or place nameText- Short name of the placeCenter- Coordinates as Vector2d (x=lat, y=lng)PlaceType- Type of place (e.g., "address", "poi", "place")Address- Street address (if applicable)Relevance- Match score (0-1)Context- Additional location context (city, state, country, etc.)
Reverse geocoding converts coordinates into a human-readable address or place name.
using UnityEngine;
using Mapbox.Unity.Map;
using Mapbox.GeocodingApi;
using Mapbox.Utils;
public class ReverseGeocodeExample : MonoBehaviour
{
public MapBehaviourCore MapCore;
private MapboxGeocodingApi _geocodingApi;
void Start()
{
MapCore.Initialized += map =>
{
_geocodingApi = new MapboxGeocodingApi(map.MapService.FileSource);
// Reverse geocode the Golden Gate Bridge location
var location = new LatitudeLongitude(37.8199, -122.4783);
GetAddressFromCoordinates(location);
};
}
void GetAddressFromCoordinates(LatitudeLongitude location)
{
var reverseGeocodeResource = new ReverseGeocodeResource(location);
_geocodingApi.Geocode(reverseGeocodeResource, (ReverseGeocodeResponse response) =>
{
if (response == null || response.Features == null || response.Features.Count == 0)
{
Debug.Log("No address found");
return;
}
var firstResult = response.Features[0];
Debug.Log($"Address: {firstResult.PlaceName}");
Debug.Log($"Type: {string.Join(", ", firstResult.PlaceType)}");
});
}
}- Waits for the map to initialize
- Creates a reverse geocoding request for coordinates (37.8199, -122.4783)
- Returns the human-readable address or place name at that location
- The first feature typically contains the most specific address
You can filter reverse geocoding results by type:
var reverseGeocodeResource = new ReverseGeocodeResource(location)
{
Types = new[] { "address" } // Only return street addresses
};Common types: country, region, postcode, place, locality, neighborhood, address, poi
The ReverseGeocodeResponse contains:
Features- List of places at or near the coordinatesQuery- The original coordinates as [longitude, latitude]Attribution- Mapbox attribution text
Each Feature includes the same fields as forward geocoding (see above).
Convert clicked map positions to addresses:
// Get world position from mouse click, convert to lat/lng, then reverse geocode
var worldPos = GetClickedWorldPosition();
var latLng = mapInformation.ConvertPositionToLatLng(worldPos);
var reverseResource = new ReverseGeocodeResource(latLng);
geocodingApi.Geocode(reverseResource, HandleReverseGeocodeResponse);Implement a location search:
var forwardResource = new ForwardGeocodeResource(searchText)
{
Autocomplete = true,
Country = new[] { "us" }
};
geocodingApi.Geocode(forwardResource, DisplaySearchResults);