My Directions API request is Invalid, but Why?

2019-06-14 05:45发布

问题:

http://maps.googleapis.com/maps/api/directions/json?origin=Central+Hong+Kong&destination=Sai+Kung&sensor=false&mode=transit

The request works fine without the mode=transit parameter, but the results only include driving directions. With the last parameter added, I get this:

"status" : "INVALID_REQUEST"

What can I do to make this work? There are definitively transit options available, they are listed in Google Maps.

回答1:

INVALID_REQUEST indicates that the provided request was invalid. Common causes of this status include an invalid parameter or parameter value.

If you set the mode to "transit" you must also specify either a departure_time or an arrival_time.

From the documentation

mode (defaults to driving) — Specifies the mode of transport to use when calculating directions. Valid values are specified in Travel Modes. If you set the mode to "transit" you must also specify either a departure_time or an arrival_time.

If "departure_time" is added to the request in your question, it returns a result:

http://maps.googleapis.com/maps/api/directions/json?origin=Central+Hong+Kong&destination=Sai+Kung&sensor=false&mode=transit&departure_time=12