Get Longitude And Latitude Of Location With Mapbox Geocoding API

June 30, 2020 by Andreas Wik

The Mapbox Geocoding API has a lot of interesting features. I used this recently when I was playing around with a Node.js app and needed to get the longitude and latitude for certain cities/locations. 

With this Geocoding API you simply provide a location string, e.g. “Berlin”, and it will return a list of locations (starting with the most relevant) and a bunch of location data, among that data you’ll find the longitude and latitude. You could then use this data to fetch the weather, for example, from a weather API which requires latitude and longitude values. Or maybe to place pins on a map.

You’ll need a free access token to use it, so head over to https://account.mapbox.com/auth/signup/ and create an account.

Once signed in, click your avatar in the top right and select account.

mapbox account navigation

 

Scroll down to the Access tokens section and hit Create a token.

mapbox tokens

 

Now, let’s get to the goodies. The URL format to get location data is as follows:

https://api.mapbox.com/geocoding/v5/mapbox.places/<YOUR_LOCATION_STRING>.json?access_token=<YOUR_ACCESS_TOKEN>

 

Replace <YOUR LOCATION STRING> with, for example, las+vegas and <YOUR_ACCESS_TOKEN> with your own token and you’re good to go.

When searching for las+vegas, Mapbox sends back the following JSON data:

{
    "type": "FeatureCollection",
    "query": [
        "las",
        "vegas"
    ],
    "features": [{
            "id": "place.2306727987317920",
            "type": "Feature",
            "place_type": [
                "place"
            ],
            "relevance": 1,
            "properties": {
                "wikidata": "Q23768"
            },
            "text": "Las Vegas",
            "place_name": "Las Vegas, Nevada, United States",
            "bbox": [
                -115.896925507127,
                35.6184504774325,
                -114.91706715939,
                36.853662
            ],
            "center": [
                -115.1492,
                36.1663
            ],
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -115.1492,
                    36.1663
                ]
            },
            "context": [{
                    "id": "region.10001740274236760",
                    "wikidata": "Q1227",
                    "short_code": "US-NV",
                    "text": "Nevada"
                },
                {
                    "id": "country.19678805456372290",
                    "short_code": "us",
                    "wikidata": "Q30",
                    "text": "United States"
                }
            ]
        },
        {
            "id": "locality.11999928169317920",
            "type": "Feature",
            "place_type": [
                "locality"
            ],
            "relevance": 1,
            "properties": {},
            "text": "Las Vegas",
            "place_name": "Las Vegas, 1700000, La Serena, Coquimbo, Chile",
            "bbox": [
                -71.2881154039999,
                -29.95096975,
                -71.2501399079999,
                -29.909731194
            ],
            "center": [
                -71.26,
                -29.91
            ],
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -71.26,
                    -29.91
                ]
            },
            "context": [{
                    "id": "postcode.12881273999909190",
                    "text": "1700000"
                },
                {
                    "id": "place.12881273999123870",
                    "wikidata": "Q14467",
                    "text": "La Serena"
                },
                {
                    "id": "region.5781167022093610",
                    "wikidata": "Q2121",
                    "short_code": "CL-CO",
                    "text": "Coquimbo"
                },
                {
                    "id": "country.10394314763731110",
                    "short_code": "cl",
                    "wikidata": "Q298",
                    "text": "Chile"
                }
            ]
        },
        {
            "id": "place.9133521562317920",
            "type": "Feature",
            "place_type": [
                "place"
            ],
            "relevance": 1,
            "properties": {
                "wikidata": "Q676171"
            },
            "text": "Las Vegas",
            "place_name": "Las Vegas, New Mexico, United States",
            "bbox": [
                -105.508706017506,
                35.2159479901684,
                -103.682777973829,
                35.781448019878
            ],
            "center": [
                -105.2239,
                35.5939
            ],
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -105.2239,
                    35.5939
                ]
            },
            "context": [{
                    "id": "region.9994833188678710",
                    "wikidata": "Q1522",
                    "short_code": "US-NM",
                    "text": "New Mexico"
                },
                {
                    "id": "country.19678805456372290",
                    "short_code": "us",
                    "wikidata": "Q30",
                    "text": "United States"
                }
            ]
        },
        {
            "id": "poi.34359796358",
            "type": "Feature",
            "place_type": [
                "poi"
            ],
            "relevance": 1,
            "properties": {
                "landmark": true,
                "wikidata": "Q6492605",
                "address": "900 Las Vegas Blvd N",
                "category": "museum, tourism"
            },
            "text": "Las Vegas Natural History Museum",
            "place_name": "Las Vegas Natural History Museum, 900 Las Vegas Blvd N, Las Vegas, Nevada 89101, United States",
            "center": [
                -115.133995,
                36.179949500000006
            ],
            "geometry": {
                "coordinates": [
                    -115.133995,
                    36.179949500000006
                ],
                "type": "Point"
            },
            "context": [{
                    "id": "neighborhood.290390",
                    "text": "Cultural Corridor"
                },
                {
                    "id": "postcode.14300447974322900",
                    "text": "89101"
                },
                {
                    "id": "place.2306727987317920",
                    "wikidata": "Q23768",
                    "text": "Las Vegas"
                },
                {
                    "id": "region.10001740274236760",
                    "wikidata": "Q1227",
                    "short_code": "US-NV",
                    "text": "Nevada"
                },
                {
                    "id": "country.19678805456372290",
                    "short_code": "us",
                    "wikidata": "Q30",
                    "text": "United States"
                }
            ]
        },
        {
            "id": "poi.850403547924",
            "type": "Feature",
            "place_type": [
                "poi"
            ],
            "relevance": 1,
            "properties": {
                "landmark": true,
                "address": "Hoşnudiye Mah. Vural Sok.",
                "category": "bar, pub, alcohol, liquor, beer"
            },
            "text": "Las Vegas Cafe & Bar",
            "place_name": "Las Vegas Cafe & Bar, Hoşnudiye Mah. Vural Sok., Eskisehir, Eskişehir, Turkey",
            "center": [
                30.514518,
                39.779747
            ],
            "geometry": {
                "coordinates": [
                    30.514518,
                    39.779747
                ],
                "type": "Point"
            },
            "context": [{
                    "id": "place.7420961685464830",
                    "wikidata": "Q168866",
                    "text": "Eskisehir"
                },
                {
                    "id": "region.6292764676256360",
                    "wikidata": "Q483053",
                    "short_code": "TR-26",
                    "text": "Eskişehir"
                },
                {
                    "id": "country.12740899366176230",
                    "short_code": "tr",
                    "wikidata": "Q43",
                    "text": "Turkey"
                }
            ]
        }
    ],
    "attribution": "NOTICE: © 2020 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained. POI(s) provided by Foursquare."
}

 

The longitude and latitude are stored in the center property of each location object: [longitude, latitude].

This is what the Mapbox geocoding api docs say about the center property:

“The coordinates of the feature’s center in the form [longitude,latitude]. This may be the literal centroid of the feature’s geometry, or the center of human activity within the feature (for example, the downtown area of a city).”

As you can see, it lists a number of different locations that matches your location search string. The top is what Mapbox finds  most relevant: Las Vegas, Nevada, United States. The second one is a place in Chile.

 

Have fun!

 

Share this article

Recommended articles

@awikio