GeekForGeek
GeekForGeek

Reputation: 11

Directions API returns different directions compared to google maps

I am using Google Directions API to find directions between an origin and a destination, for this case, the origin is a Hotel, while the destination is a metro station.

My code to get directions are

var request = {
    origin: start,
    destination: end,
    travelMode: 'DRIVING'
};
directionsService.route(request, function (result, status) {
    if (status == 'OK') {
        directionsDisplay.setDirections(result);
    }
});

After getting the directions, the map shows direction where no walking is included and the user is shown directions only via "Driving" mode, as here

A sample application to give an idea of the current scenario ....

However, getting directions from same origin to destination in google maps is showing a shorter and different route, which is using driving with some additional walking wherever possible to make the route shorter, as here

It would be great if somebody could help me out in figuring out how one can achieve directions from Point A to Point B with multiple travel modes, for my case "Driving" as well as "Walking" mode to get a shorter route.

Thanks

Upvotes: 0

Views: 1399

Answers (1)

annkylah
annkylah

Reputation: 467

I checked the demo you provided and noticed that you are using LatLng as values of your origin and destination parameters. The LatLng values you used for the origin parameter is 25.1866073,55.2803321 while the destination is at 25.2013981,55.2694649. If we use the same coordinates with Google Maps, you'll notice that you'll get the same result.

As you may notice on the link provided above, the destination coordinates you are using resolves to a location called The Private Office, Dubai. This can be the reason as to why you are getting different sets of routes.

To achieve the desired route that's the same with the Google Map result, you may either use different sets of coordinates as a value for the destination parameter like 25.201270, 55.269799 or you could also opt to use a String value for both the origin and destination parameter of the request. Here's a sample fiddle

I hope this helps!

Upvotes: 0

Related Questions