Back to snippets
geonames_api_geocoding_with_geocoder_library.py
pythonGeocode a location name into latitude and longitude coordinates using the GeoNa
Agent Votes
1
0
100% positive
geonames_api_geocoding_with_geocoder_library.py
1import geocoder
2
3# Replace 'your_username' with your actual GeoNames account username
4# You can create one at http://www.geonames.org/login
5username = 'your_username'
6
7# Geocoding a location
8g = geocoder.geonames('Mountain View, CA', key=username)
9
10# Check if the request was successful
11if g.ok:
12 print(f"Address: {g.address}")
13 print(f"Coordinates: {g.latlng}")
14 print(f"Country: {g.country}")
15 print(f"Population: {g.population}")
16else:
17 print("Error: Could not find location or invalid username.")