【问题标题】:Python code for reverse Geo-coding using Google API使用 Google API 进行反向地理编码的 Python 代码
【发布时间】:2019-06-16 16:05:56
【问题描述】:

我在一个 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}}}

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

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 API 密钥。然而,到目前为止,我发现的任何参考资料本身几乎就像一个项目,对于像我这样的初学者来说是一种过度杀伤力。 geopy 代码很好,但定位精度很差。请帮忙。

附言 我也尝试过地理编码器

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)

但是在所有情况下它都打印“无”。

【问题讨论】:

  • 不知道为什么有人将其标记为负面。它不是重复的,我试图解释我尝试过的所有事情,以便有人可以提供帮助。我还想问什么?

标签: python-2.7 google-maps reverse-geocoding


【解决方案1】:

您可以考虑从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

结果

R-1, GA Block, Sector III, Salt Lake City, Kolkata, West Bengal 700106, India

【讨论】:

  • 谢谢,在命令行中完美运行。无论如何要在地图上显示相同的内容?我有不受限制的测试 API。
  • 对于最新的 geopy 库 (2.2.0),geolocator.reverse() 调用默认使用 exactly_one=True 参数。因此代码应修改为location = geolocator.reverse("22.5757344, 88.4048656") if location: location.address
【解决方案2】:

您可以尝试使用 Python Client for Google Maps Services 库,该库位于

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

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

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))

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

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

更多详情请查看 github 中的文档。

我希望这会有所帮助!

【讨论】:

  • 由于我的 API 密钥不受限制,它显示了一个充满信息的屏幕。在向这里寻求帮助之前,我已经看过 Github,但对于像我这样的菜鸟来说,这太过分了。
猜你喜欢
  • 2015-10-30
  • 1970-01-01
  • 1970-01-01
  • 2015-11-02
  • 2021-01-26
  • 1970-01-01
  • 1970-01-01
  • 2020-06-05
  • 1970-01-01
相关资源
最近更新 更多