How to Get Latitude and Longitude from an Address
- API or service
- Forward Geocoding API
- Task
- Address → latitude and longitude
- Examples
- Difficulty
- Beginner
- Time
- 5 min
You have a postal address or place name and need its latitude and longitude—for example, to place it on a map, calculate a route, or store its location. The Geoapify Forward Geocoding API performs this address-to-coordinates conversion. This process is called forward geocoding.
Send the address either as one free-form string or as separate structured fields. The API returns matching locations with coordinates and normalized address information.
Replace
YOUR_API_KEYwith your Geoapify API key. You can also test requests in the Geocoding Playground.
Geocode a free-form address
Use text when the complete address is available as one string. This is the most convenient option for search fields, imported address lists, and user-provided text.
https://api.geoapify.com/v1/geocode/search?text=200%20Larkin%20Street%2C%20San%20Francisco%2C%20CA%2094102%2C%20United%20States&format=json&limit=1&apiKey=YOUR_API_KEY
The first result contains lat and lon.
Reduced response: This live response keeps the coordinates and normalized address fields. The full response also contains datasource, timezone, plus-code, and result-quality information.
{
"results": [
{
"name": "Asian Art Museum of San Francisco",
"housenumber": "200",
"street": "Larkin Street",
"city": "San Francisco",
"state": "California",
"state_code": "CA",
"postcode": "94102",
"country": "United States",
"country_code": "us",
"formatted": "Asian Art Museum of San Francisco, 200 Larkin Street, San Francisco, CA 94102, United States of America",
"address_line1": "Asian Art Museum of San Francisco",
"address_line2": "200 Larkin Street, San Francisco, CA 94102, United States of America",
"lat": 37.780315,
"lon": -122.4159922,
"result_type": "amenity"
}
]
}
Geocode a structured address
Use structured parameters when the address already exists in separate form or database fields. Send one or more of name, housenumber, street, postcode, city, state, and country.
https://api.geoapify.com/v1/geocode/search?housenumber=200&street=Larkin%20Street&postcode=94102&city=San%20Francisco&state=California&country=United%20States&format=json&limit=1&apiKey=YOUR_API_KEY
Reduced response: This live response keeps the coordinates and normalized address fields. The full response also contains datasource, timezone, plus-code, and result-quality information.
{
"results": [
{
"name": "Asian Art Museum of San Francisco",
"housenumber": "200",
"street": "Larkin Street",
"city": "San Francisco",
"state": "California",
"state_code": "CA",
"postcode": "94102",
"country": "United States",
"country_code": "us",
"formatted": "Asian Art Museum of San Francisco, 200 Larkin Street, San Francisco, CA 94102, United States of America",
"address_line1": "Asian Art Museum of San Francisco",
"address_line2": "200 Larkin Street, San Francisco, CA 94102, United States of America",
"lat": 37.780315,
"lon": -122.4159922,
"result_type": "amenity"
}
]
}
Free-form and structured inputs are alternatives. Do not combine text with structured address parameters in the same request.
Geocode an address in an application
Use this approach in a browser application or in Node.js 18 and later. It uses the built-in fetch() API to send the request and URLSearchParams to encode the address safely. It works well when coordinates are needed immediately after a form submission, address selection, or other user action.
const params = new URLSearchParams({
text: "200 Larkin Street, San Francisco, CA 94102, United States",
format: "json",
limit: "1",
apiKey: "YOUR_API_KEY"
});
const response = await fetch(
`https://api.geoapify.com/v1/geocode/search?${params}`
);
if (!response.ok) {
throw new Error(`Geocoding failed: ${response.status}`);
}
const data = await response.json();
const result = data.results[0];
console.log({ latitude: result.lat, longitude: result.lon });
In browser applications, restrict the API key to the application’s allowed origins.
Geocode an address in a script
Use this approach for backend services, data-processing jobs, command-line tools, or automation scripts. It uses Python’s standard-library urllib and json modules, so no third-party package is required.
import json
from urllib.parse import urlencode
from urllib.request import urlopen
params = urlencode({
"text": "200 Larkin Street, San Francisco, CA 94102, United States",
"format": "json",
"limit": 1,
"apiKey": "YOUR_API_KEY",
})
url = f"https://api.geoapify.com/v1/geocode/search?{params}"
with urlopen(url) as response:
data = json.load(response)
result = data["results"][0]
print({"latitude": result["lat"], "longitude": result["lon"]})
Always check that results is not empty before using the first item in production code.