【问题标题】:Geocoded_Waypoints Google Maps Api Zero ResultsGeocoded_Waypoints Google Maps Api 零结果
【发布时间】:2019-09-23 22:06:08
【问题描述】:

我正在使用带有此 python 代码的 google maps api 来打印两点之间的路线。

import requests, json
#Google MapsDdirections API endpoint
endpoint = 'https://maps.googleapis.com/maps/api/directions/json?'
api_key = 'AIzaSyCTPkufBttRcfSkA9zPYgivrYs9QEhdEEU'
#Asks the user to input Where they are and where they want to go.
origin = input('Where are you?: ').replace(' ','+')
destination = input('Where do you want to go?: ').replace(' ','+')
#Building the URL for the request
nav_request = 'origin={}&destination={}&key={}'.format(origin,destination,api_key)
request = endpoint + nav_request
#Sends the request and reads the response.
#response = urllib.request.urlopen(request).read()
r = requests.get('https://maps.googleapis.com/maps/api/directions/json?origin=Vigo&destination=Lugo&key=AIzaSyCTPkufBttRcfSkA9zPYgivrYs9QEhdEEU')
#Loads response as JSON
#directions = json.loads(response)
directions = r.json()
print(directions)

问题是我的回复给了我零结果。 我已经在谷歌浏览器中手动尝试过,得到下一个结果:

{
   "geocoded_waypoints" : [
      {
         "geocoder_status" : "OK",
         "place_id" : "ChIJbYcwsYDOMQ0RDAVnKL9fMB8",
         "types" : [ "locality", "political" ]
      },
      {
         "geocoder_status" : "OK",
         "place_id" : "ChIJk8GyYRRiLw0Rn9RLF60dRHs",
         "types" : [ "locality", "political" ]
      }
   ],
   "routes" : [
      {
         "bounds" : {
            "northeast" : {
               "lat" : 43.0082848,
               "lng" : -7.554997200000001
            },
            "southwest" : {
               "lat" : 42.2392374,
               "lng" : -8.720694999999999
            }
         },
         "copyrights" : "Datos de mapas ©2019 Inst. Geogr. Nacional",
         "legs" : [
            {
               "distance" : {
                  "text" : "188 km",
                  "value" : 188311
               },
               "duration" : {
                  "text" : "2h 11 min",
                  "value" : 7830
               },
               "end_address" : "Vigo, Pontevedra, España",
               "end_location" : {
                  "lat" : 42.2406168,
                  "lng" : -8.720694999999999
               },
               "start_address" : "Lugo, España",
               "start_location" : {
                  "lat" : 43.0082848,
[...]

但是,当我在线尝试时,我得到了不同的地理编码航点,因此,zero_results。

'types': ['bar', 'establishment', 'food', 'point_of_interest', 'restaurant']}], 'routes': [], 'status': 'ZERO_RESULTS'}

如何更改 geocoded_waypoint 类型??

【问题讨论】:

    标签: python json api google-maps request


    【解决方案1】:

    从您的示例中我可以看出,您尝试获取两个西班牙城市之间的路线。但是,当您仅指定始发地和目的地的城市名称时,由于参数不明确,服务可能会将它们解析为不同的国家/地区。例如,当我在位于美国的服务器上执行您的请求时,目标 Lugo 被解析为放置 ID ChIJi59iTw6wZIgRvssCUK9Ra84,这是一家名为“Lugo's”的餐厅,位于 107 S Main St, Dickson, TN 37055, USA。

    查看地点详情

    https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJi59iTw6wZIgRvssCUK9Ra84&fields=formatted_address,geometry,name,type&key=YOUR_API_KEY

    由于起点位于西班牙,终点位于美国,因此路线服务无法建立行车路线并返回 ZERO_RESULTS。

    为了解决歧义,您应该提供更精确的起点和终点参数,或者指定搜索结果所在的区域。

    如果我在请求中添加 region 参数,我会得到西班牙城市之间的预期路线

    https://maps.googleapis.com/maps/api/directions/json?origin=Vigo&destination=Lugo&region=ES&key=YOUR_API_KEY

    您可以在路线计算器中看到它:

    https://directionsdebug.firebaseapp.com/?origin=Vigo&destination=Lugo&region=ES

    希望我的回答能澄清你的疑惑!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-30
      • 2014-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-10
      相关资源
      最近更新 更多