10
votes

I have searched the documentation in Google Directions API, and also looked around online and cannot find an answer to my conundrum. My agency has developed an API to find the latest possible departure between origins and destinations using transit, and there are a few which are returning "No_Result" errors. However, I am able to return results when I search Google Maps manually.

My Google Directions API program sends out the following query:

https://maps.googleapis.com/maps/api/directions/json?units=imperial&origin=650+Memorial+Dr+chicopee+MA&destination=50+College+St+South+Hadley+MA&arrival_time=1461301200&mode=transit

which returns:

    {
   "available_travel_modes" : [ "DRIVING", "BICYCLING", "WALKING" ],
   "geocoded_waypoints" : [
      {
         "geocoder_status" : "OK",
         "place_id" : "ChIJ7VtqLK7d5okR-bTUfKuHVpo",
         "types" : [ "street_address" ]
      },
      {
         "geocoder_status" : "OK",
         "place_id" : "ChIJxwHLSqzb5okR1rrjYhcDvkc",
         "types" : [ "premise" ]
      }
   ],
   "routes" : [],
   "status" : "ZERO_RESULTS"
}

However, when I query manually using Google maps, I am able to return a result via transit.

https://www.google.com/maps/dir/650+Memorial+Drive,+Chicopee,+MA/50+College+Street,+South+Hadley,+MA/@42.2678007,-72.7164286,11z/data=!3m1!4b1!4m18!4m17!1m5!1m1!1s0x89e6ddae2c6a5bed:0x9a5687ab7cd4b4f9!2m2!1d-72.5797548!2d42.1751992!1m5!1m1!1s0x89e6dbac4b2aac81:0xe9809aca8e8e0bdc!2m2!1d-72.5766752!2d42.2538136!2m3!6e1!7e2!8j1461200400!3e3

I have double-checked any obvious mistakes (like using "Rd" instead of "St", or inputting an address that doesn't exist) but have not found any. I have also changed the departure/arrival times in the program, with no luck.

The error message looks like it is indicating that MODE as TRANSIT is not an option between that origin/destination pair - but then the manual interface doesn't have an issue.

Does anyone know why Google Maps might be rejecting the API query, but not the manual query? Or any resources to help figure out this problem? Thanks!!

3
Maybe you hit an area that is "not supported" through the API (for whatever reason...). Same is the case with Tokyo, see here: issuetracker.google.com/issues/35826181Patrick Boos

3 Answers

5
votes

The 'All caps' response is (unintentionally, but) misleadingly wrong - the Google Maps API server doesn't do proper error checking here, and if you give it an all-caps mode parameter it will return driving directions no matter which mode you wrote.

Real answer is that the API doesn't support all modes in all places, even though the Google Maps app does. You can file feature requests to try to push for this parity. For example, public transit in Japan: https://issuetracker.google.com/issues/35826181

Test queries to demonstrate that Maps Directions responses don't care which mode you specify if you do it in all-caps (be sure to add your API key):

Driving - https://maps.googleapis.com/maps/api/directions/json?departure_time=now&destination=place_id%3AChIJp4QhcgzyGGARZaBIPuJzfpg&mode=DRIVING&origin=place_id%3AChIJlyOpErWHGGAR0156e32g1Xs&key=API_KEY

Transit - https://maps.googleapis.com/maps/api/directions/json?departure_time=now&destination=place_id%3AChIJp4QhcgzyGGARZaBIPuJzfpg&mode=TRANSIT&origin=place_id%3AChIJlyOpErWHGGAR0156e32g1Xs&key=API_KEY

Walking - https://maps.googleapis.com/maps/api/directions/json?departure_time=now&destination=place_id%3AChIJp4QhcgzyGGARZaBIPuJzfpg&mode=WALKING&origin=place_id%3AChIJlyOpErWHGGAR0156e32g1Xs&key=API_KEY

4
votes

The mode (mode=transit) in your query string should be capitalized - try mode=TRANSIT instead.

0
votes

I think this is helpful for this error .Actually the main problem is let and long are not correctly formatted and position changed.so apply this code. and enjoy

var service = new google.maps.DistanceMatrixService();
                                        service.getDistanceMatrix({
                                            origins: [latOffice + "," + longOffice],
                                            destinations: [latitudeS + "," + longitudeS],
                                            travelMode: google.maps.TravelMode.DRIVING,
                                            unitSystem: google.maps.UnitSystem.METRIC,
                                            avoidHighways: true,
                                            avoidTolls: true
                                        }, function (response, status) {
                                            if (status == google.maps.DistanceMatrixStatus.OK && response.rows[0].elements[0].status != "ZERO_RESULTS") {
                                                var distance2 = response.rows[0].elements[0].distance.text;
                                                var duration2 = response.rows[0].elements[0].duration.text;
                                                duration = duration2;
                                            } else {
                                                //alert("Your Request For Distance Not Available");
                                                Swal.fire('Oops...', 'Your Request For Distance Not Available!', 'error');
                                            }