How to Get an Address from Latitude and Longitude
- API or service
- Reverse Geocoding API
- Task
- Latitude and longitude → address, city, or postcode
- Examples
- Difficulty
- Beginner
- Time
- 5 min
You have a latitude and longitude—for example, from a map click, device location, or stored point—and need a readable address or an individual address field. The Geoapify Reverse Geocoding API finds the address or place associated with those coordinates. This process is called reverse geocoding.
Use the default request for the nearest complete address. Request a result type such as city or postcode when the application needs a location at that specific level.
Replace
YOUR_API_KEYwith your Geoapify API key. Latitude ranges from-90to90; longitude ranges from-180to180.
Reverse geocode coordinates
The following coordinates point to Old Saint Stephens Church in Boston:
https://api.geoapify.com/v1/geocode/reverse?lat=42.365344&lon=-71.052655&format=json&apiKey=YOUR_API_KEY
The first result provides coordinates, a display-ready address, and normalized address components.
Reduced response: This live response keeps the nearest location and normalized address fields. The full response also contains datasource, timezone, plus-code, and result-quality information.
{
"results": [
{
"name": "Old Saint Stephens Church",
"housenumber": "24",
"street": "Clark Street",
"city": "Boston",
"county": "Suffolk County",
"state": "Massachusetts",
"state_code": "MA",
"postcode": "02109",
"country": "United States",
"country_code": "us",
"formatted": "Old Saint Stephens Church, 24 Clark Street, Boston, MA 02109, United States of America",
"address_line1": "Old Saint Stephens Church",
"address_line2": "24 Clark Street, Boston, MA 02109, United States of America",
"lat": 42.3653541,
"lon": -71.0526773,
"result_type": "amenity",
"distance": 0
}
]
}
Get the city for coordinates
Set type=city when the application needs a city-level result instead of the nearest building or address.
https://api.geoapify.com/v1/geocode/reverse?lat=42.365344&lon=-71.052655&type=city&format=json&apiKey=YOUR_API_KEY
Read the city from results[0].city and check results[0].result_type to understand the returned location level.
Reduced response: This live response keeps the city and its containing administrative fields. The full response contains additional metadata.
{
"results": [
{
"name": "Boston",
"city": "Boston",
"county": "Suffolk County",
"state": "Massachusetts",
"state_code": "MA",
"country": "United States",
"country_code": "us",
"formatted": "Boston, MA, United States of America",
"address_line1": "Boston, MA",
"address_line2": "United States of America",
"lat": 42.3588336,
"lon": -71.0578303,
"result_type": "city",
"distance": 0
}
]
}
Get the postcode for coordinates
Set type=postcode to request the postcode associated with the coordinates.
https://api.geoapify.com/v1/geocode/reverse?lat=42.365344&lon=-71.052655&type=postcode&format=json&apiKey=YOUR_API_KEY
Read the postcode from results[0].postcode and confirm that result_type is postcode.
Reduced response: This live response keeps the postcode feature and its normalized address fields. The full response contains additional metadata.
{
"results": [
{
"city": "Boston",
"county": "Suffolk County",
"state": "Massachusetts",
"state_code": "MA",
"postcode": "02113",
"country": "United States",
"country_code": "us",
"formatted": "North End, Boston, MA 02113, United States of America",
"address_line1": "North End",
"address_line2": "Boston, MA 02113, United States of America",
"lat": 42.365113247,
"lon": -71.055059003,
"result_type": "postcode",
"distance": 199.16799174119285
}
]
}
The default address-level request above returns postcode 02109 for the church. With type=postcode, the API instead returns the nearest postcode-level feature, which is 02113 in this example. Its distance is measured in metres from the requested coordinates. Use the default request when you need the postcode field of the nearest address; use type=postcode when you specifically need a postcode-level result. Coverage and precision depend on the available source data, so handle an empty result or missing postcode.
Get an address with JavaScript
Use this approach in browser applications or Node.js 18 and later. The function accepts latitude first and longitude second, uses the built-in fetch() API, and returns the complete first result so the caller can use either formatted or individual address fields.
async function getAddress(latitude, longitude) {
const params = new URLSearchParams({
lat: String(latitude),
lon: String(longitude),
format: "json",
apiKey: "YOUR_API_KEY"
});
const response = await fetch(
`https://api.geoapify.com/v1/geocode/reverse?${params}`
);
if (!response.ok) {
throw new Error(`Reverse geocoding failed: ${response.status}`);
}
const data = await response.json();
return data.results[0] ?? null;
}
const address = await getAddress(42.365344, -71.052655);
console.log(address?.formatted ?? "No address found");
Always handle both HTTP errors and an empty results array. In browser applications, restrict the API key to the application’s allowed origins.
Get an address with Python
Use this approach in backend services, automation scripts, or data-processing jobs. It uses Python’s standard-library urllib and json modules, so no third-party dependency is required.
import json
from urllib.parse import urlencode
from urllib.request import urlopen
params = urlencode({
"lat": 42.365344,
"lon": -71.052655,
"format": "json",
"apiKey": "YOUR_API_KEY",
})
url = f"https://api.geoapify.com/v1/geocode/reverse?{params}"
with urlopen(url) as response:
data = json.load(response)
address = data["results"][0] if data["results"] else None
print(address["formatted"] if address else "No address found")
urlopen() raises an HTTPError for unsuccessful responses. Catch it when the application needs retries or custom error reporting, and continue checking for an empty results array as shown above.