我需要将经度和纬度坐标转换为国家或城市,python 中是否有这样的示例?
提前致谢!
原文由 godzilla 发布,翻译遵循 CC BY-SA 4.0 许可协议
我需要将经度和纬度坐标转换为国家或城市,python 中是否有这样的示例?
提前致谢!
原文由 godzilla 发布,翻译遵循 CC BY-SA 4.0 许可协议
谷歌此后删除了对其 API 的无密钥访问。前往谷歌并注册一个密钥,您每天可以获得约 1,000 个免费查询。接受的答案中的代码应该像这样修改(不能添加评论,没有足够的代表)。
from urllib.request import urlopen
import json
def getplace(lat, lon):
key = "yourkeyhere"
url = "https://maps.googleapis.com/maps/api/geocode/json?"
url += "latlng=%s,%s&sensor=false&key=%s" % (lat, lon, key)
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))
原文由 tibernut 发布,翻译遵循 CC BY-SA 4.0 许可协议
4 回答4.4k 阅读✓ 已解决
4 回答3.8k 阅读✓ 已解决
1 回答3k 阅读✓ 已解决
3 回答2.1k 阅读✓ 已解决
1 回答4.4k 阅读✓ 已解决
1 回答3.8k 阅读✓ 已解决
1 回答2.8k 阅读✓ 已解决
我使用谷歌的 API。
输出: