'Python module for getting latitude and longitude from the name of a US city
I am looking for a python module which can take in the name of the city as the input and return the latitude and longitude of the input.
Solution 1:[1]
An example:
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent='myapplication')
location = geolocator.geocode("Chicago Illinois")
print(location.address)
Available info:
>>> location.raw
{u'display_name': u'Chicago, Cook County, Illinois, United States of America', u
'importance': 1.0026476104889, u'place_id': u'97957568', u'lon': u'-87.6243706',
u'lat': u'41.8756208', u'osm_type': u'relation', u'licence': u'Data \xa9 OpenSt
reetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright', u'osm_i
d': u'122604', u'boundingbox': [u'41.6439170837402', u'42.0230255126953', u'-87.
9401016235352', u'-87.5239791870117'], u'type': u'city', u'class': u'place', u'i
con': u'http://nominatim.openstreetmap.org/images/mapicons/poi_place_city.p.20.p
ng'}
Solution 2:[2]
You can also use geocoder with Bing Maps API. The API gets latitude and longitude data for all addresses for me (unlike Nominatim) and it has a pretty good free version for non-commercial uses (max 125000 requests per year for free). To get free API, go here and click "Get a free Basic key". After getting your API, you can use it in the code below:
import geocoder # pip install geocoder
g = geocoder.bing('Tokyo', key='<API KEY>')
results = g.json
print(results['lat'], results['lng'])
Output:
>>> 35.68696212768555 139.7494659423828
The results contains much more information than longitude and latitude. Check it out.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Sayyor Y |
