Google Directions API 公交始终返回“ZERO_RESULTS”

Lor*_*oni 5 google-maps google-directions-api travel-time

我正在开发一个应用程序,它使用谷歌 API 来获取交通解决方案,以便在所需的时间到达某个位置。

\n\n

我调用 google API 的代码与“驾驶”和“步行”出行模式完美配合,但对于“公交”和“骑自行车”,响应始终标记为状态“ZERO_RESULTS”,并且不计算任何路线。\n这由google 文档,意味着在必须计算解决方案的国家/地区不提供公共交通和骑自行车服务。\n我尝试了多个国家/地区,但总是得到相同的结果,我还尝试了概述中的 Google 示例(针对公共交通)和它不起作用。

\n\n

我通过 HTTPS 请求传递的参数是:出发地、目的地、出行方式、到达时间。\n我尝试的第一件事是更改到达时间,以防该值指的是过去或太遥远的未来,但它不起作用。

\n\n

如果我没有通过“arrival_time”,响应是完美的,但我不明白,因为在 Google 文档中很清楚,arrival_time 是 https 请求的一个可能参数,也适用于过境旅行模式。如果没有这个论点,这个请求对我的目标来说毫无用处。

\n\n

奇怪的是,如果我通过“过境”(带有到达或出发时间),则响应是这样的:

\n\n
{\n   "available_travel_modes" : [ "DRIVING", "WALKING" ],\n   "geocoded_waypoints" : [\n      {\n         "geocoder_status" : "OK",\n         "place_id" : "ChIJu46S-ZZhLxMROG5lkwZ3D7k",\n         "types" : [ "locality", "political" ]\n      },\n      {\n         "geocoder_status" : "OK",\n         "place_id" : "ChIJ53USP0nBhkcRjQ50xhPN_zw",\n         "types" : [ "locality", "political" ]\n      }\n   ],\n   "routes" : [],\n   "status" : "ZERO_RESULTS"\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果我通过“骑自行车”(所有其他参数不变):

\n\n
{\n   "available_travel_modes" : [ "DRIVING", "WALKING", "TRANSIT" ],\n   "geocoded_waypoints" : [\n      {\n         "geocoder_status" : "OK",\n         "place_id" : "ChIJu46S-ZZhLxMROG5lkwZ3D7k",\n         "types" : [ "locality", "political" ]\n      },\n      {\n         "geocoder_status" : "OK",\n         "place_id" : "ChIJ53USP0nBhkcRjQ50xhPN_zw",\n         "types" : [ "locality", "political" ]\n      }\n   ],\n   "routes" : [],\n   "status" : "ZERO_RESULTS"\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

所以在第二种情况下,API 说 TRANSIT 可用,但这根本没有意义!

\n\n

我使用的位置格式是地理坐标,以避免出现歧义。

\n\n

希望我提供足够的信息以使您能够帮助我。

\n\n

编辑:抱歉,我以为我的帖子已经足够清楚了。\n我使用 Google Directions API: https: //developers.google.com/maps/documentation/directions/

\n\n

我的问题是“过境”和“骑自行车”作为出行模式,不是代码问题,但问题出现在 https 响应级别,即使使用“过境”的谷歌示例(可以在我获得的概述中找到) “ZERO_RESULTS”响应。\n这是示例的 URL:

\n\n
https://maps.googleapis.com/maps/api/directions/json?origin=75+9th+Ave+New+York,+NY&destination=MetLife+Stadium+1+MetLife+Stadium+Dr+East+Rutherford,+NJ+07073&mode=transit&arrival_time=1391374800&key=YOUR_API_KEY\n
Run Code Online (Sandbox Code Playgroud)\n\n

下面是两个 URL,它们让我得到了上面复制的两个响应:\n与过境:

\n\n
https://maps.googleapis.com/maps/api/directions/json?origin=Rome&destination=Milan&mode=transit&arrival_time=1513162800000&key=++API_KEY++\n
Run Code Online (Sandbox Code Playgroud)\n\n

以及骑自行车:

\n\n
https://maps.googleapis.com/maps/api/directions/json?origin=Rome&destination=Milan&mode=transit&arrival_time=1513162800000&key=++API_KEY++\n
Run Code Online (Sandbox Code Playgroud)\n\n

在我用于起点和目的地地理坐标的代码中,如下所示:

\n\n
https://maps.googleapis.com/maps/api/directions/json?origin=41.9027835,12.496365500000024&destination=45.4642035,9.189981999999986&mode=transit&arrival_time=1513162800000&key=++API_KEY++\n
Run Code Online (Sandbox Code Playgroud)\n\n

唯一的区别是谷歌不识别地理编码的航点:

\n\n
{\n   "available_travel_modes" : [ "DRIVING", "WALKING" ],\n   "geocoded_waypoints" : [ {}, {} ],\n   "routes" : [],\n   "status" : "ZERO_RESULTS"\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果我使用“驾驶”作为旅行模式,这有效:

\n\n
https://maps.googleapis.com/maps/api/directions/json?origin=41.9027835,12.496365500000024&destination=45.4642035,9.189981999999986&mode=driving&arrival_time=1513162800000&key=++API_KEY++\n\n{\n   "geocoded_waypoints" : [\n      {\n         "geocoder_status" : "OK",\n         "place_id" : "EjJQaWF6emEgZGVsbGEgUmVwdWJibGljYSwgMTAsIDAwMTg1IFJvbWEgUk0sIEl0YWxpYQ",\n         "types" : [ "street_address" ]\n      },\n      {\n         "geocoder_status" : "OK",\n         "place_id" : "ChIJC5u9LazGhkcRXAZQFNDTpKc",\n         "types" : [ "street_address" ]\n      }\n   ],\n   "routes" : [\n      {\n         "bounds" : {\n            "northeast" : {\n               "lat" : 45.4731019,\n               "lng" : 12.6177051\n            },\n            "southwest" : {\n               "lat" : 41.9028523,\n               "lng" : 9.1839517\n            }\n         },\n         "copyrights" : "Dati mappa \xc2\xa92017 Google",\n         "legs" : [\n            {\n               "distance" : {\n                  "text" : "573 km",\n                  "value" : 572600\n               },\n               "duration" : {\n                  "text" : "5 ore 51 min",\n                  "value" : 21069\n               },\n               "end_address" : "Via Silvio Pellico, 2, 20121 Milano MI, Italia",\n               "end_location" : {\n                  "lat" : 45.46495119999999,\n                  "lng" : 9.1892874\n               },\n               "start_address" : "Piazza della Repubblica, 10, 00185 Roma RM, Italia",\n               "start_location" : {\n                  "lat" : 41.9028523,\n                  "lng" : 12.4964704\n               },\n               "steps" : [\n                  {\n                     "distance" : {\n                        "text" : "93 m",\n                        "value" : 93\n                     },\n                     "duration" : {\n                        "text" : "1 min",\n                        "value" : 19\n                     },\n                     "end_location" : {\n                        "lat" : 41.9035781,\n                        "lng" : 12.4959908\n                     },\n                     "html_instructions" : "Procedi in direzione \\u003cb\\u003enord\\u003c/b\\u003e da \\u003cb\\u003ePiazza della Repubblica\\u003c/b\\u003e verso \\u003cb\\u003eVia Giuseppe Romita\\u003c/b\\u003e",\n                     "polyline" : {\n                        "points" : "ycw~F}ugkAU?I@E@E@a@RG@EDED_@d@OP"\n                     },\n                     "start_location" : {\n                        "lat" : 41.9028523,\n                        "lng" : 12.4964704\n                     },\n                     "travel_mode" : "DRIVING"\n                  },\n                  // cut\n                  {\n                     "distance" : {\n                        "text" : "52 m",\n                        "value" : 52\n                     },\n                     "duration" : {\n                        "text" : "1 min",\n                        "value" : 14\n                     },\n                     "end_location" : {\n                        "lat" : 45.4665738,\n                        "lng" : 9.1889772\n                     },\n                     "html_instructions" : "Continua su \\u003cb\\u003eVia Santa Margherita\\u003c/b\\u003e",\n                     "polyline" : {\n                        "points" : "ogotGoxaw@`@`@j@h@"\n                     },\n                     "start_location" : {\n                        "lat" : 45.4669622,\n                        "lng" : 9.189359399999999\n                     },\n                     "travel_mode" : "DRIVING"\n                  },\n                  {\n                     "distance" : {\n                        "text" : "0,2 km",\n                        "value" : 192\n                     },\n                     "duration" : {\n                        "text" : "1 min",\n                        "value" : 87\n                     },\n                     "end_location" : {\n                        "lat" : 45.46495119999999,\n                        "lng" : 9.1892874\n                     },\n                     "html_instructions" : "Svolta a \\u003cb\\u003esinistra\\u003c/b\\u003e e prendi \\u003cb\\u003eVia Silvio Pellico\\u003c/b\\u003e",\n                     "maneuver" : "turn-left",\n                     "polyline" : {\n                        "points" : "aeotGcvaw@Xw@DCBAxC@~A?d@A"\n                     },\n                     "start_location" : {\n                        "lat" : 45.4665738,\n                        "lng" : 9.1889772\n                     },\n                     "travel_mode" : "DRIVING"\n                  }\n               ],\n               "traffic_speed_entry" : [],\n               "via_waypoint" : []\n            }\n         ],\n         "overview_polyline" : {\n            "points" : "//collapsed//"\n         },\n         "summary" : "A1/E35 e A1",\n         "warnings" : [],\n         "waypoint_order" : []\n      }\n   ],\n   "status" : "OK"\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果我使用“transit”而没有到达时间,它会起作用:

\n\n
 https://maps.googleapis.com/maps/api/directions/json?origin=41.9027835,12.496365500000024&destination=45.4642035,9.\n189981999999986&mode=transit&key=++API_KEY++\n
Run Code Online (Sandbox Code Playgroud)\n\n

和:

\n\n
{\n   "geocoded_waypoints" : [\n      {\n         "geocoder_status" : "OK",\n         "place_id" : "EjJQaWF6emEgZGVsbGEgUmVwdWJibGljYSwgMTAsIDAwMTg1IFJvbWEgUk0sIEl0YWxpYQ",\n         "types" : [ "street_address" ]\n      },\n      {\n         "geocoder_status" : "OK",\n         "place_id" : "ChIJRYxePKzGhkcRsgPwamn2Pfo",\n         "types" : [ "street_address" ]\n      }\n   ],\n   "routes" : [\n      {\n         "bounds" : {\n            "northeast" : {\n               "lat" : 45.4969308,\n               "lng" : 12.5528041\n            },\n            "southwest" : {\n               "lat" : 41.892252,\n               "lng" : 9.188611\n            }\n         },\n         "copyrights" : "Dati mappa \xc2\xa92017 Google",\n         "legs" : [\n            {\n               "arrival_time" : {\n                  "text" : "13:55",\n                  "time_zone" : "Europe/Rome",\n                  "value" : 1513169733\n               },\n               "departure_time" : {\n                  "text" : "10:10",\n                  "time_zone" : "Europe/Rome",\n                  "value" : 1513156231\n               },\n               "distance" : {\n                  "text" : "571 km",\n                  "value" : 571467\n               },\n               "duration" : {\n                  "text" : "3 ore 45 min",\n                  "value" : 13502\n               },\n               "end_address" : "Piazza del Duomo, 1, 20121 Milano MI, Italia",\n               "end_location" : {\n                  "lat" : 45.4639037,\n                  "lng" : 9.188611\n               },\n               "start_address" : "Piazza della Repubblica, 10, 00185 Roma RM, Italia",\n               "start_location" : {\n                  "lat" : 41.9028429,\n                  "lng" : 12.4964793\n               },\n               "steps" : [\n                  {\n                     "distance" : {\n                        "text" : "0,1 km",\n                        "value" : 102\n                     },\n                     "duration" : {\n                        "text" : "1 min",\n                        "value" : 87\n                     },\n                     "end_location" : {\n                        "lat" : 41.902193,\n                        "lng" : 12.4958041\n                     },\n                     "html_instructions" : "Cammina fino a Repubblica",\n                     "polyline" : {\n                        "points" : "wcw~F_vgkA@CBADABA@A@?B?D?B?B@B?B@BBB@@BBBDH@D@H@@?D?BDRBL?@DXBLBJDHBFVM"\n                     },\n                     "start_location" : {\n                        "lat" : 41.9028429,\n                        "lng" : 12.4964793\n                     },\n                     "steps" : [\n                        {\n                           "distance" : {\n                              "text" : "54 m",\n                              "value" : 54\n                           },\n                           "duration" : {\n                              "text" : "1 min",\n                              "value" : 49\n                           },\n                           "end_location" : {\n                              "lat" : 41.9024775,\n                              "lng" : 12.4962611\n                           },\n                           "html_instructions" : "Procedi in direzione \\u003cb\\u003esudest\\u003c/b\\u003e verso \\u003cb\\u003ePiazza della Repubblica\\u003c/b\\u003e",\n                           "polyline" : {\n                              "points" : "wcw~F_vgkA@CBADABA@A@?B?D?B?B@B?B@BBB@@BBBDH@D@H@@?D?B"\n                           },\n                           "start_location" : {\n                              "lat" : 41.9028429,\n                              "lng" : 12.4964793\n                           },\n                           "travel_mode" : "WALKING"\n                        },\n                        {\n                           "distance" : {\n                              "text" : "48 m",\n                              "value" : 48\n                           },\n                           "duration" : {\n                              "text" : "1 min",\n                              "value" : 38\n                           },\n                           "end_location" : {\n                              "lat" : 41.902193,\n                              "lng" : 12.4958041\n                           },\n                           "html_instructions" : "Esci dalla rotonda e prendi \\u003cb\\u003ePiazza della Repubblica\\u003c/b\\u003e",\n                           "polyline" : {\n                              "points" : "oaw~FstgkADRBL?@DXBLBJDHBFVM"\n                           },\n                           "start_location" : {\n                              "lat" : 41.9024775,\n                              "lng" : 12.4962611\n                           },\n                           "travel_mode" : "WALKING"\n                        }\n                     ],\n                     "travel_mode" : "WALKING"\n                  },\n                  {\n                     "distance" : {\n                        "text" : "0,4 km",\n                        "value" : 437\n                     },\n                     "duration" : {\n                        "text" : "1 min",\n                        "value" : 46\n                     },\n                     "end_location" : {\n                        "lat" : 41.9011732,\n                        "lng" : 12.4996019\n                     },\n                     "html_instructions" : "Metropolitana verso Anagnina",\n                     "polyline" : {\n                        "points" : "u_w~FwqgkAeBIBoA@I|ByM~CmELR"\n                     },\n                     "start_location" : {\n                        "lat" : 41.902193,\n                        "lng" : 12.4958041\n                     },\n                     "transit_details" : {\n                        "arrival_stop" : {\n                           "location" : {\n                              "lat" : 41.9011732,\n                              "lng" : 12.4996019\n                           },\n                           "name" : "Termini"\n                        },\n                        "arrival_time" : {\n                           "text" : "10:12",\n                           "time_zone" : "Europe/Rome",\n                           "value" : 1513156353\n                        },\n                        "departure_stop" : {\n                           "location" : {\n                              "lat" : 41.902193,\n                              "lng" : 12.4958041\n                           },\n                           "name" : "Repubblica"\n                        },\n                        "departure_time" : {\n                           "text" : "10:11",\n                           "time_zone" : "Europe/Rome",\n                           "value" : 1513156307\n                        },\n                        "headsign" : "Anagnina",\n                        "line" : {\n                           "agencies" : [\n                              {\n                                 "name" : "Atac",\n                                 "phone" : "011 39 06 57003",\n                                 "url" : "http://www.atac.roma.it/"\n                              }\n                           ],\n                           "color" : "#e27439",\n                           "name" : "Metro A",\n                           "short_name" : "MEA",\n                           "url" : "http://muovi.roma.it/percorso/js?query=MEA&cl=1",\n                           "vehicle" : {\n                              "icon" : "//maps.gstatic.com/mapfiles/transit/iw2/6/subway2.png",\n                              "local_icon" : "//maps.gstatic.com/mapfiles/transit/iw2/6/it-metro.png",\n                              "name" : "Metropolitana",\n                              "type" : "SUBWAY"\n                           }\n                        },\n                        "num_stops" : 1\n                     },\n                     "travel_mode" : "TRANSIT"\n                  },\n\n                  {\n                     "distance" : {\n                        "text" : "2,6 km",\n                        "value" : 2641\n                     },\n                     "duration" : {\n                        "text" : "6 min",\n                        "value" : 330\n                     },\n                     "end_location" : {\n                        "lat" : 45.4639037,\n                        "lng" : 9.188611\n                     },\n                     "html_instructions" : "Metropolitana verso San Donato",\n                     "polyline" : {\n                        "points" : "//collapsed//"\n                     },\n                     "start_location" : {\n                        "lat" : 45.4844397,\n                        "lng" : 9.202612799999999\n                     },\n                     "transit_details" : {\n                        "arrival_stop" : {\n                           "location" : {\n                              "lat" : 45.4639037,\n                              "lng" : 9.188611\n                           },\n                           "name" : "Duomo"\n                        },\n                        "arrival_time" : {\n                           "text" : "13:55",\n                           "time_zone" : "Europe/Rome",\n                           "value" : 1513169730\n                        },\n                        "departure_stop" : {\n                           "location" : {\n                              "lat" : 45.4844397,\n                              "lng" : 9.202612799999999\n                           },\n                           "name" : "Centrale FS"\n                        },\n                        "departure_time" : {\n                           "text" : "13:50",\n                           "time_zone" : "Europe/Rome",\n                           "value" : 1513169400\n                        },\n                        "headsign" : "San Donato",\n                        "line" : {\n                           "agencies" : [\n                              {\n                                 "name" : "COMUNE DI MILANO",\n                                 "phone" : "011 39 02 0202",\n                                 "url" : "http://www.muoversi.milano.it/"\n                              }\n                           ],\n                           "color" : "#ffea00",\n                           "name" : "M3 - Linea Gialla",\n                           "short_name" : "3",\n                           "vehicle" : {\n                              "icon" : "//maps.gstatic.com/mapfiles/transit/iw2/6/subway2.png",\n                              "local_icon" : "//maps.gstatic.com/mapfiles/transit/iw2/6/it-metro.png",\n                              "name" : "Metropolitana",\n                              "type" : "SUBWAY"\n                           }\n                        },\n                        "num_stops" : 4\n                     },\n                     "travel_mode" : "TRANSIT"\n                  }\n               ],\n               "traffic_speed_entry" : [],\n               "via_waypoint" : []\n            }\n         ],\n         "overview_polyline" : {\n            "points" : "//collapsed//"\n         },\n         "summary" : "",\n         "

xom*_*ena 4

您请求中的问题是您作为arrival_time。请注意,该值必须以秒为单位:

\n\n
\n

到达时间\xe2\x80\x94 指定公交路线所需的到达时间(以秒为单位)。您可以指定出发时间或到达时间,但不能同时指定两者。请注意,arrival_time 必须指定为整数。

\n
\n\n

https://developers.google.com/maps/documentation/directions/intro#DirectionsRequests

\n\n

您正在传递1513162800000对应于 的毫秒03/22/49920 @ 8:00am (UTC),正确的值是1513162800对应于12/13/2017 @ 11:00am (UTC)。

\n\n

请求应该是:

\n\n

https://maps.googleapis.com/maps/api/directions/json?origin=41.9027835%2C12.496365500000024&destination=45.4642035%2C9.189981999999986&mode=transit&arrival_time=1513162800&key=YOUR_API_KEY

\n\n

路线计算器中的相同请求:

\n\n

https://directionsdebug.firebaseapp.com/?origin=41.9027835%2C12.496365500000024&destination=45.4642035%2C9.189981999999986&mode=transit&arrival_time=1513162800

\n\n

我希望这有帮助!

\n