google 将方向 return 映射到原点

google maps directions return to origin

我正在尝试获取 origin -> waypoint -> destination
的路线 除一项特定要求外,一切正常:

Origin (Point A) : PH Benidor, Calle Gaspar O. Hernández, Panamá
Waypoint (Point B): BAC Credomatic | Sucursal Plaza New York, Calle 50, Panamá
Destination (Point A): PH Benidor, Calle Gaspar O. Hernández, Panamá

因此,如果我从 A -> B 发出请求 - 一切正常,使 B -> A 也可以正常工作。 A -> A 也有效。一旦我为那个特定的 (B) 地址做了一个航路点,google API returns ZERO_RESULT.

检查URL:https://maps.googleapis.com/maps/api/directions/json?key=<API_KEY>&mode=driving&origin=PH+Benidor%2C+Calle+Gaspar+O.+Hern%C3%A1ndez%2C+Panam%C3%A1&destination=PH+Benidor%2C+Calle+Gaspar+O.+Hern%C3%A1ndez%2C+Panam%C3%A1&alternatives=true&units=metric&waypoints=BAC+Credomatic+|+Sucursal+Plaza+New+York%2C+Calle+50%2C+Panam%C3%A1

更新
地址取自 Google 自动完成 API 字段,因此我假设它们的格式正确。来自 A -> B 的路由适用于相同的地址
https://maps.googleapis.com/maps/api/directions/json?key=API_KEY&alternatives=true&units=metric&mode=driving&origin=PH+Benidor%2C+Calle+Gaspar+O.+Hern%C3%A1ndez%2C+Panam%C3%A1&destination=BAC+Credomatic+|+Sucursal+Plaza+New+York%2C+Calle+50%2C+Panam%C3%A1

回应

{
   "geocoded_waypoints" : [
      {
         "geocoder_status" : "OK",
         "place_id" : "ChIJxX3EIvqorI8RRhwI3jYajZc",
         "types" : [ "premise" ]
      },
      {
         "geocoder_status" : "OK",
         "place_id" : "ChIJMR2dd-OorI8ROKhRZctCf0k",
         "types" : [
            "bus_station",
            "establishment",
            "point_of_interest",
            "transit_station"
         ]
      }
   ],
   "routes" : [
      {
         "bounds" : {
            "northeast" : {
               "lat" : 8.9896817,
               "lng" : -79.52026149999999
            },
            "southwest" : {
               "lat" : 8.979656199999999,
               "lng" : -79.52709229999999
            }
         },
         "copyrights" : "Картографические данные © 2017 Google",
         "legs" : [
            {
               "distance" : {
                  "text" : "2,1 км",
                  "value" : 2052
               },
               "duration" : {
                  "text" : "8 мин.",
                  "value" : 480
               },
               "end_address" : "Calle 50, Panamá, Панама",
               "end_location" : {
                  "lat" : 8.982186499999999,
                  "lng" : -79.52026149999999
               },
               "start_address" : "PH Benidor, Calle Gaspar O. Hernández, Panamá, Панама",
               "start_location" : {
                  "lat" : 8.9896817,
                  "lng" : -79.5251863
               },
               "steps" : [
                  {
                     "distance" : {
                        "text" : "0,1 км",
                        "value" : 111
                     },
                     "duration" : {
                        "text" : "1 мин.",
                        "value" : 50
                     },
                     "end_location" : {
                        "lat" : 8.9887005,
                        "lng" : -79.52522759999999
                     },
                     "html_instructions" : "Направляйтесь на \u003cb\u003eюг\u003c/b\u003e по \u003cb\u003eCalle Gaspar O. Hernández\u003c/b\u003e в сторону \u003cb\u003eVía Argentina\u003c/b\u003e",
                     "polyline" : {
                        "points" : "oxzu@lgkdNbDR^K"
                     },
                     "start_location" : {
                        "lat" : 8.9896817,
                        "lng" : -79.5251863
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0,2 км",
                        "value" : 241
                     },
                     "duration" : {
                        "text" : "2 мин.",
                        "value" : 99
                     },
                     "end_location" : {
                        "lat" : 8.987735199999999,
                        "lng" : -79.52327099999999
                     },
                     "html_instructions" : "Поверните \u003cb\u003eналево\u003c/b\u003e на \u003cb\u003eVía Argentina\u003c/b\u003e",
                     "maneuver" : "turn-left",
                     "polyline" : {
                        "points" : "krzu@tgkdNDKr@wBLc@N[b@kAr@}AHQBE@A"
                     },
                     "start_location" : {
                        "lat" : 8.9887005,
                        "lng" : -79.52522759999999
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0,7 км",
                        "value" : 737
                     },
                     "duration" : {
                        "text" : "2 мин.",
                        "value" : 128
                     },
                     "end_location" : {
                        "lat" : 8.982404600000001,
                        "lng" : -79.52709229999999
                     },
                     "html_instructions" : "Поверните \u003cb\u003eнаправо\u003c/b\u003e на \u003cb\u003eVía España\u003c/b\u003e",
                     "maneuver" : "turn-right",
                     "polyline" : {
                        "points" : "klzu@l{jdNf@Hl@Nb@Ld@Lj@Xn@`@z@t@Zd@JRHJT\PVPPNNVNj@^\P`FzCh@VhAp@j@p@@@pCxA"
                     },
                     "start_location" : {
                        "lat" : 8.987735199999999,
                        "lng" : -79.52327099999999
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0,4 км",
                        "value" : 424
                     },
                     "duration" : {
                        "text" : "2 мин.",
                        "value" : 105
                     },
                     "end_location" : {
                        "lat" : 8.979656199999999,
                        "lng" : -79.5244422
                     },
                     "html_instructions" : "Поверните \u003cb\u003eналево\u003c/b\u003e на \u003cb\u003eCalle Aquilino de la Guardia\u003c/b\u003e",
                     "maneuver" : "turn-left",
                     "polyline" : {
                        "points" : "_kyu@hskdNBE@ADE`CcDp@w@d@i@j@e@xBeBd@[~@s@p@a@"
                     },
                     "start_location" : {
                        "lat" : 8.982404600000001,
                        "lng" : -79.52709229999999
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0,5 км",
                        "value" : 539
                     },
                     "duration" : {
                        "text" : "2 мин.",
                        "value" : 98
                     },
                     "end_location" : {
                        "lat" : 8.982186499999999,
                        "lng" : -79.52026149999999
                     },
                     "html_instructions" : "Поверните \u003cb\u003eналево\u003c/b\u003e на \u003cb\u003eCalle 50\u003c/b\u003e/\u003cb\u003eCalle Nicanor de Obarrio\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eПункт назначения будет справа\u003c/div\u003e",
                     "maneuver" : "turn-left",
                     "polyline" : {
                        "points" : "{yxu@vbkdNkAcCmAgCOWm@kACECCg@gAm@aA}CcGe@{@"
                     },
                     "start_location" : {
                        "lat" : 8.979656199999999,
                        "lng" : -79.5244422
                     },
                     "travel_mode" : "DRIVING"
                  }
               ],
               "traffic_speed_entry" : [],
               "via_waypoint" : []
            }
         ],
         "overview_polyline" : {
            "points" : "oxzu@lgkdNbDR^KDK`A{Cr@gB|@oBDGtAXhAZj@Xn@`@z@t@Zd@T^f@t@`@`@bAn@~FlDrBhAl@r@pCxABEFGrD{EpAoA~CaC~@s@p@a@yCkGeAmBuAiCcE_I"
         },
         "summary" : "Vía España и Calle 50/Calle Nicanor de Obarrio",
         "warnings" : [],
         "waypoint_order" : []
      },
      {
         "bounds" : {
            "northeast" : {
               "lat" : 8.9896817,
               "lng" : -79.52026149999999
            },
            "southwest" : {
               "lat" : 8.9806346,
               "lng" : -79.52528719999999
            }
         },
         "copyrights" : "Картографические данные © 2017 Google",
         "legs" : [
            {
               "distance" : {
                  "text" : "2,1 км",
                  "value" : 2149
               },
               "duration" : {
                  "text" : "10 мин.",
                  "value" : 588
               },
               "end_address" : "Calle 50, Panamá, Панама",
               "end_location" : {
                  "lat" : 8.982186499999999,
                  "lng" : -79.52026149999999
               },
               "start_address" : "PH Benidor, Calle Gaspar O. Hernández, Panamá, Панама",
               "start_location" : {
                  "lat" : 8.9896817,
                  "lng" : -79.5251863
               },
               "steps" : [
                  {
                     "distance" : {
                        "text" : "0,1 км",
                        "value" : 111
                     },
                     "duration" : {
                        "text" : "1 мин.",
                        "value" : 50
                     },
                     "end_location" : {
                        "lat" : 8.9887005,
                        "lng" : -79.52522759999999
                     },
                     "html_instructions" : "Направляйтесь на \u003cb\u003eюг\u003c/b\u003e по \u003cb\u003eCalle Gaspar O. Hernández\u003c/b\u003e в сторону \u003cb\u003eVía Argentina\u003c/b\u003e",
                     "polyline" : {
                        "points" : "oxzu@lgkdNbDR^K"
                     },
                     "start_location" : {
                        "lat" : 8.9896817,
                        "lng" : -79.5251863
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0,2 км",
                        "value" : 241
                     },
                     "duration" : {
                        "text" : "2 мин.",
                        "value" : 99
                     },
                     "end_location" : {
                        "lat" : 8.987735199999999,
                        "lng" : -79.52327099999999
                     },
                     "html_instructions" : "Поверните \u003cb\u003eналево\u003c/b\u003e на \u003cb\u003eVía Argentina\u003c/b\u003e",
                     "maneuver" : "turn-left",
                     "polyline" : {
                        "points" : "krzu@tgkdNDKr@wBLc@N[b@kAr@}AHQBE@A"
                     },
                     "start_location" : {
                        "lat" : 8.9887005,
                        "lng" : -79.52522759999999
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0,2 км",
                        "value" : 238
                     },
                     "duration" : {
                        "text" : "1 мин.",
                        "value" : 43
                     },
                     "end_location" : {
                        "lat" : 8.985976599999999,
                        "lng" : -79.52439769999999
                     },
                     "html_instructions" : "Поверните \u003cb\u003eнаправо\u003c/b\u003e на \u003cb\u003eVía España\u003c/b\u003e",
                     "maneuver" : "turn-right",
                     "polyline" : {
                        "points" : "klzu@l{jdNf@Hl@Nb@Ld@Lj@Xn@`@z@t@Zd@JR"
                     },
                     "start_location" : {
                        "lat" : 8.987735199999999,
                        "lng" : -79.52327099999999
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0,2 км",
                        "value" : 231
                     },
                     "duration" : {
                        "text" : "1 мин.",
                        "value" : 59
                     },
                     "end_location" : {
                        "lat" : 8.9846278,
                        "lng" : -79.5227939
                     },
                     "html_instructions" : "Поверните \u003cb\u003eналево\u003c/b\u003e на \u003cb\u003eCalle 52 Este\u003c/b\u003e",
                     "maneuver" : "turn-left",
                     "polyline" : {
                        "points" : "kazu@nbkdNFGR]pF{G"
                     },
                     "start_location" : {
                        "lat" : 8.985976599999999,
                        "lng" : -79.52439769999999
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0,1 км",
                        "value" : 149
                     },
                     "duration" : {
                        "text" : "1 мин.",
                        "value" : 62
                     },
                     "end_location" : {
                        "lat" : 8.9853383,
                        "lng" : -79.52164499999999
                     },
                     "html_instructions" : "Поверните \u003cb\u003eналево\u003c/b\u003e на \u003cb\u003eAv. Samuel Lewis\u003c/b\u003e",
                     "maneuver" : "turn-left",
                     "polyline" : {
                        "points" : "}xyu@lxjdNWe@MSMQyAyC"
                     },
                     "start_location" : {
                        "lat" : 8.9846278,
                        "lng" : -79.5227939
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0,2 км",
                        "value" : 183
                     },
                     "duration" : {
                        "text" : "1 мин.",
                        "value" : 41
                     },
                     "end_location" : {
                        "lat" : 8.9839468,
                        "lng" : -79.52075069999999
                     },
                     "html_instructions" : "Поверните \u003cb\u003eнаправо\u003c/b\u003e на \u003cb\u003eCalle 54 Este\u003c/b\u003e",
                     "maneuver" : "turn-right",
                     "polyline" : {
                        "points" : "k}yu@fqjdNpGmDBC"
                     },
                     "start_location" : {
                        "lat" : 8.9853383,
                        "lng" : -79.52164499999999
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0,3 км",
                        "value" : 332
                     },
                     "duration" : {
                        "text" : "1 мин.",
                        "value" : 74
                     },
                     "end_location" : {
                        "lat" : 8.982836499999999,
                        "lng" : -79.5233909
                     },
                     "html_instructions" : "Поверните \u003cb\u003eнаправо\u003c/b\u003e на перекрестке 1 на \u003cb\u003eAv. Ricardo Arango\u003c/b\u003e",
                     "maneuver" : "turn-right",
                     "polyline" : {
                        "points" : "utyu@tkjdNzAlC@@rCvESfD"
                     },
                     "start_location" : {
                        "lat" : 8.9839468,
                        "lng" : -79.52075069999999
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "88 м",
                        "value" : 88
                     },
                     "duration" : {
                        "text" : "1 мин.",
                        "value" : 19
                     },
                     "end_location" : {
                        "lat" : 8.982181299999999,
                        "lng" : -79.52383399999999
                     },
                     "html_instructions" : "Поверните \u003cb\u003eналево\u003c/b\u003e на \u003cb\u003eAvenida Ricardo Arango\u003c/b\u003e",
                     "maneuver" : "turn-left",
                     "polyline" : {
                        "points" : "wmyu@d|jdNbCvA"
                     },
                     "start_location" : {
                        "lat" : 8.982836499999999,
                        "lng" : -79.5233909
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0,2 км",
                        "value" : 197
                     },
                     "duration" : {
                        "text" : "1 мин.",
                        "value" : 58
                     },
                     "end_location" : {
                        "lat" : 8.9806346,
                        "lng" : -79.523155
                     },
                     "html_instructions" : "Поверните \u003cb\u003eналево\u003c/b\u003e на \u003cb\u003eCalle Beatriz M. de Cabal\u003c/b\u003e",
                     "maneuver" : "turn-left",
                     "polyline" : {
                        "points" : "siyu@|~jdN~BoB~AS`AAJ?F?"
                     },
                     "start_location" : {
                        "lat" : 8.982181299999999,
                        "lng" : -79.52383399999999
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "61 м",
                        "value" : 61
                     },
                     "duration" : {
                        "text" : "1 мин.",
                        "value" : 26
                     },
                     "end_location" : {
                        "lat" : 8.9809009,
                        "lng" : -79.52267329999999
                     },
                     "html_instructions" : "Поверните \u003cb\u003eналево\u003c/b\u003e в сторону \u003cb\u003eCalle 50\u003c/b\u003e/\u003cb\u003eCalle Nicanor de Obarrio\u003c/b\u003e",
                     "maneuver" : "turn-left",
                     "polyline" : {
                        "points" : "}_yu@vzjdNMYg@gA"
                     },
                     "start_location" : {
                        "lat" : 8.9806346,
                        "lng" : -79.523155
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "19 м",
                        "value" : 19
                     },
                     "duration" : {
                        "text" : "1 мин.",
                        "value" : 7
                     },
                     "end_location" : {
                        "lat" : 8.9807585,
                        "lng" : -79.52257179999999
                     },
                     "html_instructions" : "Поверните \u003cb\u003eнаправо\u003c/b\u003e в сторону \u003cb\u003eCalle 50\u003c/b\u003e/\u003cb\u003eCalle Nicanor de Obarrio\u003c/b\u003e",
                     "maneuver" : "turn-right",
                     "polyline" : {
                        "points" : "sayu@twjdNZQ?A"
                     },
                     "start_location" : {
                        "lat" : 8.9809009,
                        "lng" : -79.52267329999999
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0,3 км",
                        "value" : 299
                     },
                     "duration" : {
                        "text" : "1 мин.",
                        "value" : 50
                     },
                     "end_location" : {
                        "lat" : 8.982186499999999,
                        "lng" : -79.52026149999999
                     },
                     "html_instructions" : "Поверните \u003cb\u003eналево\u003c/b\u003e на \u003cb\u003eCalle 50\u003c/b\u003e/\u003cb\u003eCalle Nicanor de Obarrio\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eПункт назначения будет справа\u003c/div\u003e",
                     "maneuver" : "turn-left",
                     "polyline" : {
                        "points" : "w`yu@`wjdNCCg@gAm@aA}CcGe@{@"
                     },
                     "start_location" : {
                        "lat" : 8.9807585,
                        "lng" : -79.52257179999999
                     },
                     "travel_mode" : "DRIVING"
                  }
               ],
               "traffic_speed_entry" : [],
               "via_waypoint" : []
            }
         ],
         "overview_polyline" : {
            "points" : "oxzu@lgkdNbDR^KDK`A{Cr@gB|@oBDGtAXhAZj@Xn@`@z@t@Zd@JRFGdGyHe@y@MQyAyCtGqD|AnCrCvESfDbCvA~BoB~AS`AAR?u@aBZSk@kAkEeIe@{@"
         },
         "summary" : "Av. Ricardo Arango",
         "warnings" : [],
         "waypoint_order" : []
      }
   ],
   "status" : "OK"
}

所以我假设地址有问题,如果它在 waypoints 参数中。

我检查了路线 API 请求中的出发地、目的地和 waypoints 来自你的问题

https://maps.googleapis.com/maps/api/directions/json?origin=PH%20Benidor%2C%20Calle%20Gaspar%20O.%20Hern%C3%A1ndez%2C%20Panam%C3%A1&destination=PH%20Benidor%2C%20Calle%20Gaspar%20O.%20Hern%C3%A1ndez%2C%20Panam%C3%A1&mode=driving&waypoints=BAC%20Credomatic%7CSucursal%20Plaza%20New%20York%2C%20Calle%2050%2C%20Panam%C3%A1&key=MY_API_KEY

响应包含以下地理编码 waypoints:

"geocoded_waypoints":[
{
  "geocoder_status":"OK",
  "place_id":"ChIJxX3EIvqorI8RRhwI3jYajZc",
  "types":[
    "premise"
  ]
},
{
  "geocoder_status":"OK",
  "place_id":"ChIJZ1RH5IDoaY8RcqGCRNAAPQw",
  "types":[
    "establishment","finance","point_of_interest"
  ]
},
{
  "geocoder_status":"OK",
  "place_id":"ChIJf1seg-OorI8R_66YzaG7hko",
  "types":[
    "bank","establishment","finance","food","point_of_interest","restaurant"
  ]
},
{
  "geocoder_status":"OK",
  "place_id":"ChIJxX3EIvqorI8RRhwI3jYajZc",
  "types":[
    "premise"
  ]
}]

让我们来看看地点ID为ChIJZ1RH5IDoaY8RcqGCRNAAPQw的航路点。它对应于您请求中的'BAC Credomatic'。如果您使用地理编码器工具检查地点 ID,您会看到 'BAC Credomatic' 被解析为洪都拉斯的营业地点,该地点位于岛上,无法驾车到达。

https://google-developers.appspot.com/maps/documentation/utils/geocoder/#place_id%3DChIJZ1RH5IDoaY8RcqGCRNAAPQw

这是 ZERO_RESULTS 的原因。指定地址 waypoints 时必须更精确。查看以下说明如何正确设置地址字符串格式的文档:

https://developers.google.com/maps/faq#geocoder_queryformat

希望对您有所帮助!