Over a million developers have joined DZone.

Mapbox’s API to Geocode Data to Get Location Information

DZone's Guide to

Mapbox’s API to Geocode Data to Get Location Information

A tutorial on using MapBox's API for finding national park location data.

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.


When starting the National Geographic project, the dataset that was being used had missing data. The dataset was missing street, city, state or zipcode to go along with the longitude and latitude for parks. Also, when doing a general search of street, city, state or zipcode, we wanted the capability to get the longitude and latitude of the search term to supply the nearest parks.  So I looked in to using the Mapbox Geocoding API to solve the issues. This API allows users to easily change location text into graphic coordinates (Maryland → -76.349,38.841) by forward geocoding. Reverse geocoding is the opposite wherein a pair of coordinates, latitude and longitude, are converted into a particular name of a place(-110.547,44.596 →Yellowstone National Park).

Forward Geocoding

In forward geocoding,  the query should be URL-coded and must not contain special characters such as semicolons that are often used to group or delimit requests. The response is usually made up of one or more results.

Here is what the URL looks like:

https://api.mapbox.com/geocoding/v5/mapbox.places/Query.json?access_token=<access token>

Here is an example use of the URL with Query = Maryland:

https://api.mapbox.com/v4/geocode/mapbox.places/Maryland.json?access_token=<access token>

Here is sample of the response:

{"type":"FeatureCollection","query":["maryland"],"features":[{"id":"region.9113309487783240","type":"Feature","text":"Maryland","place_name":"Maryland, United States","relevance":0.99,"properties":{},"bbox":[-79.487651,37.886605,-74.8966568003481,39.722883],"center":[-76.349402,38.841628],"geometry":{"type":"Point","coordinates":[-76.349402,38.841628]}

To get the rest of the data, you will actually have to make the http request in a browser or with a programming language. Also, with forward geocoding, country filtering and with proximity can be added. If you want more information about how to use the API to forward geocode, you can got here.

Reverse Geocoding

Reverse geocoding latitude and longitude coordinates as input. The response includes one result from each geocoding dataset.

Here is what the URL looks like:

https://api.mapbox.com/v4/geocode/mapbox.places/'+ xCoordinate+ ',' + yCoordinate + '.json?access_token=<access token>

Here is an example use of the URL with the x-coordinate/longitude = -110.547 and y coordinate/latitude = 44.596:

https://api.mapbox.com/v4/geocode/mapbox.places/-110.547,44.596.json.json?access_token=<access token>

Here is sample of the response:

{"type":"FeatureCollection","query":[-110.547,44.596],"features":[{"id":"place.55914","type":"Feature","text":"Yellowstone National Park","place_name":"Yellowstone National Park, Wyoming, United States","relevance":1,"properties":{},"bbox":[-111.056889009999,44.1312909900081,-109.825486991003,45.0039050099999],"center":[-110.83,44.46],"geometry":{"type":"Point","coordinates":[-110.83,44.46]},"context":[{"id":"postcode.8237635196769770","text":"82190"},{"id":"region.10947505346724150","text":"Wyoming"},{"id":"country.5877825732302570","text":"United States","short_code":"us"}]}

To get the rest of the data, you will actually have to make the http request in a browser or with a programming language. Limiting the input coordinates to at most 5 decimal places usually improves precision and performance of the reverse geocoding process. If you want more information about using the API to reverse geocode, you can go here.

Forward and reverse geocoding can be further enhanced by filtering to match more types. Features such as country, region, postcode, place, neighborhood, address and POI (places of interest) may be used. In the end, Mapbox's API allowed us to geocode data to get location information.

If you are interested in discussing more about how to use Mapbox's API in your software applications, leave a comment, contact me here or at adetola@adelabs.com.

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

mapbox ,api ,geocoding ,geodata

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}