-2

Very simple question. I'm trying to pass to the directions API to give preference to the nearest destination to the origin. E.G. : Origin : Heleman Halls (In utah) Dest : Chic fil a

When I try this specific example Maps API it selects a chic fil a that is out of state and is 17 hours away.

My SDK is node.

I've checked the documentation and haven't found anything referencing to setting the default destination to be the nearest to the origin. I know I can be more specific in my API requests (like specifying which city the dest is) but it completely defeats the purpose of my implementation.

function getMapsData(userOrigin, userDest) {

  googleMapsClient.directions(
    {
      origin: userOrigin,
      destination: userDest
    },
    function(err, response) {
      if (!err) {
        // return response;
        console.log("Got the maps data");
        formatMapsData(response.json.routes[0].legs[0]);
      }
    }
  );
}

I expect it to return just a few steps as there's a chic fil a just down the road, but instead it returns a chic fil a thats 17 hours away.

Kevin Young
  • 51
  • 1
  • 9

1 Answers1

1

This is because the Directions API's Origin and Destination use Geocoding in order to identify the locations.

If you input exact addresses like this :

https://maps.googleapis.com/maps/api/directions/json?origin=Helaman%20Halls%2C%20Provo%2C%20UT%2084604%2C%20USA&destination=Chick-fil-A%2C%20484%20W%20Bulldog%20Blvd%2C%20Provo%2C%20UT%2084604%2C%20USA&mode=driving&units=metric&avoid=tolls&key=YOUR_API_KEY

Then the Directions API will return the proper result.

It would be best to use Places API's Nearby Search or Autocomplete to first identify the origin and destination of your request before dynamically building your Directions API request.

Waren
  • 335
  • 2
  • 9