【问题标题】:Python Google Maps Geocoding APIPython 谷歌地图地理编码 API
【发布时间】:2018-02-08 21:09:20
【问题描述】:

我正在通过 Python 库使用 Google 的地理编码 API(已试用 GeoPy 1.11.0)。 对于诸如“Utica”之类的查询,我得到一个响应:

{u'geometry': {u'location_type': u'APPROXIMATE', u'bounds': {u'northeast': 
{u'lat': 43.132269, u'lng': -75.1609059}, u'southwest': {u'lat': 
43.0644689, u'lng': -75.295296}}, u'viewport': {u'northeast': {u'lat': 
43.132269, u'lng': -75.1609059}, u'southwest': {u'lat': 43.0644689, u'lng': 
-75.295296}}, u'location': {u'lat': 43.100903, u'lng': -75.232664}}, 
u'address_components': [{u'long_name': u'Utica', u'types': [u'locality', 
u'political'], u'short_name': u'Utica'}, {u'long_name': u'Oneida County', 
u'types': [u'administrative_area_level_2', u'political'], u'short_name': 
u'Oneida County'}, {u'long_name': u'New York', u'types': 
[u'administrative_area_level_1', u'political'], u'short_name': u'NY'}, 
{u'long_name': u'United States', u'types': [u'country', u'political'], 
u'short_name': u'US'}], u'place_id': u'ChIJKXZqNVE32YkRhvztGCY2GhE', 
u'formatted_address': u'Utica, NY, USA', u'types': [u'locality', 
u'political']}

问题在于美国有多个 Utica,例如 Utica, NY 和 Utica, WI。我希望 Google 会返回所有可能以 Utica 为名的地方,有没有办法做到这一点?

以下是使用 Google 的 GeoPy 的代码:

locations = ["Utica"]
from geopy.geocoders import GoogleV3
geolocator = GoogleV3(api_key=GoogleAPIKey)
for location in locations:
    print location
    results = geolocator.geocode(query=location, language='en', exactly_one=False, timeout=5)
        for result in results:
            print result.raw

【问题讨论】:

    标签: python google-maps geocoding google-geocoding-api geopy


    【解决方案1】:

    恐怕没有办法从执行网络服务请求的 Google 数据库中获取所有可能具有特定名称的地理特征。

    请注意,地理编码 API 的目的是将地址字符串解析为其坐标。通常,Geocoding API Web 服务将仅提供一个结果,即根据 Google 标准(显着性、内部评分等)与地址字符串最佳匹配。如果您的搜索字符串不明确,例如“Utica”,Geocoding Web 服务将选择最突出的结果,显然是“Utica, NY, USA”。

    作为解决问题的方法,您可以尝试使用 Places API autocomplete 请求。自动完成请求将为您的搜索地址字符串返回最多 5 条建议。不幸的是,它不能保证所有可能的地理特征,但您至少会拥有其中的 5 个。

    查看以下在美国搜索名称为“Utica”的地区的请求:

    https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Utica&types=(cities)&components=country%3AUS&key=YOUR_API_KEY

    此请求返回以下预测:

    • 美国纽约州尤蒂卡(地点 ID ChIJKXZqNVE32YkRhvztGCY2GhE)
    • 美国密歇根州尤蒂卡(地点 ID ChIJz8RnUrndJIgRNUbzGmaEdTk)
    • 美国俄亥俄州尤蒂卡(地点 ID ChIJ1ajvLWQwOIgR6Axda9Ip9XA)
    • 美国伊利诺伊州尤蒂卡(地点 ID ChIJdeJyBUlZCYgRpPiIgT2O7gE)
    • 美国肯塔基州尤蒂卡(地点 ID ChIJzbKUniCWb4gRj_IDgwlk-N0)

    最后,由于您使用的是 Python,我建议您使用 Google Maps API Web Services 的 Python 客户端库 来执行地点自动完成请求:

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

    此客户端库的文档位于:

    https://googlemaps.github.io/google-maps-services-python/docs/

    我希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2011-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多