Get Coordinates of a Location via Mapbox’s Geocoding API
Got Python? Got 5 minutes? Want to know the exact coordinates of a location you have in mind for free, such as the image below? Then let’s get into it.
Download the relevant libraries:
Query the API:
This defines a function that takes in a query and your Mapbox API key in order to query Mapbox’s geocoding API: https://docs.mapbox.com/api/search/geocoding/
Don’t have an API key? Get one here: https://docs.mapbox.com/help/getting-started/access-tokens/
Explanation
In this code snippet we are using the function, queryMapBox, to look for MIT Cambridge. From the output of the queryMapBox function, we load the data into a readable json format using the json.loads() function. Then, normalize that json data into a DataFrame using the json_normalize() function. The results:
Done! Happy Coding.