首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Google API进行反向地理编码的Python代码

使用Google API进行反向地理编码的Python代码
EN

Stack Overflow用户
提问于 2019-01-23 06:05:55
回答 2查看 8.7K关注 0票数 3

我在一个json文件(geo.json)中获取地理数据,该文件具有以下结构

代码语言:javascript
复制
 {"userId":"Geo-data","data":{"mocked":false,"timestamp":1548173963281,"coords":{"speed":0,"heading":0,"accuracy":20.20400047302246,"longitude":88.4048656,"altitude":0,"latitude":22.5757344}}}

我只想打印与上述数据相对应的地点细节,如果可能的话,也可以在地图上显示出来。

代码语言:javascript
复制
from geopy.geocoders import Nominatim
geolocator = Nominatim()
location = geolocator.reverse("22.5757344, 88.4048656")
print(location.address)
print((location.latitude, location.longitude))

但是我得到的位置不是很精确。在https://www.latlong.net/Show-Latitude-Longitude.html中,相同的坐标给出了很好的结果,但是我也有一个Google键。然而,到目前为止,我所发现的参考资料几乎就像一个项目本身,对于像我这样的初学者来说,这是一个过分的选择。位势码很好,但定位精度很低。请帮帮忙。

P.S .我也尝试过地理编码器

代码语言:javascript
复制
import geocoder
g = geocoder.google([45.15, -75.14], method='reverse')
print(g.city)
print(g.state)
print(g.state_long)
print(g.country)
print(g.country_long)

然而,它在所有情况下都打印“无”。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-01-30 12:22:50

您可以考虑从OpenStreetMap Nominatim提供程序切换到Google地理编码。然后,下面的示例似乎返回您期望的地址:

代码语言:javascript
复制
from geopy.geocoders import GoogleV3

geolocator = GoogleV3(api_key=google_key)
locations = geolocator.reverse("22.5757344, 88.4048656")
if locations:
    print(locations[0].address)  # select first location

结果

代码语言:javascript
复制
R-1, GA Block, Sector III, Salt Lake City, Kolkata, West Bengal 700106, India
票数 4
EN

Stack Overflow用户

发布于 2019-01-30 18:50:33

您可以尝试一个用于Google服务库的Python客户端,可以在

https://github.com/googlemaps/google-maps-services-python

这是由Google开发的Googlers web服务请求的包装库。代码快照如下

代码语言:javascript
复制
import googlemaps
gmaps = googlemaps.Client(key='Add Your Key here')

# Look up an address with reverse geocoding
reverse_geocode_result = gmaps.reverse_geocode((22.5757344, 88.4048656))

此代码返回address R-1, GA Block, Sector III, Salt Lake City, Kolkata, West Bengal 700106, India,类似于您可以在Geo编码器工具中看到的结果:

https://developers-dot-devsite-v2-prod.appspot.com/maps/documentation/utils/geocoder/#q%3D22.575734%252C88.404866

有关更多细节,请参阅github中的文档。

我希望这能帮到你!

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54320931

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档