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

Pra*_*tik 4 google-maps reverse-geocoding python-2.7

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

 {"userId":"Geo-data","data":{"mocked":false,"timestamp":1548173963281,"coords":{"speed":0,"heading":0,"accuracy":20.20400047302246,"longitude":88.4048656,"altitude":0,"latitude":22.5757344}}}
Run Code Online (Sandbox Code Playgroud)

我想要的只是打印与上述数据相对应的地点详细信息,如果可能的话,也将其显示在地图上。我已使用 geopy 尝试了以下代码

from geopy.geocoders import Nominatim
geolocator = Nominatim()
location = geolocator.reverse("22.5757344, 88.4048656")
print(location.address)
print((location.latitude, location.longitude))
Run Code Online (Sandbox Code Playgroud)

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

PS我也尝试过地理编码器

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)
Run Code Online (Sandbox Code Playgroud)

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

Vad*_*hev 5

您可以考虑从OpenStreetMap Nominatim提供商切换到Google Geocoding。然后,以下示例似乎返回您期望的地址:

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
Run Code Online (Sandbox Code Playgroud)

结果

R-1, GA Block, Sector III, Salt Lake City, Kolkata, West Bengal 700106, India
Run Code Online (Sandbox Code Playgroud)


xom*_*ena 5

您可以尝试使用 Google 地图服务库的 Python 客户端,该库位于

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

这是由 Google 员工开发的 Google Maps API Web 服务请求的包装库。代码快照如下

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))
Run Code Online (Sandbox Code Playgroud)

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

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

有关更多详细信息,请查看 github 中的文档。

我希望这有帮助!