我使用Google的API。
from urllib2 import urlopenimport jsondef getplace(lat, lon): url = "http://maps.googleapis.com/maps/api/geopre/json?" url += "latlng=%s,%s&sensor=false" % (lat, lon) v = urlopen(url).read() j = json.loads(v) components = j['results'][0]['address_components'] country = town = None for c in components: if "country" in c['types']: country = c['long_name'] if "postal_town" in c['types']: town = c['long_name'] return town, countryprint(getplace(51.1, 0.1))print(getplace(51.2, 0.1))print(getplace(51.3, 0.1))
输出:
(u'Hartfield', u'United Kingdom')(u'Edenbridge', u'United Kingdom')(u'Sevenoaks', u'United Kingdom')
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)