gpt4 book ai didi

python - 如何将经度和纬度转换为国家或城市?

转载 作者:IT老高 更新时间:2023-10-28 20:31:53 36 4
gpt4 key购买 nike

我需要将经度和纬度坐标转换为国家或城市,python中有这样的例子吗?

提前致谢!

最佳答案

我使用 Google 的 API。

from urllib2 import urlopen
import json
def getplace(lat, lon):
url = "http://maps.googleapis.com/maps/api/geocode/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, country


print(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')

关于python - 如何将经度和纬度转换为国家或城市?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20169467/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com