0
votes

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

1
Looks like the code you supplied is using the DIrectionsService, not the DistanceMatrix as stated in the title. Which is it? In general, Google Maps and the Google Maps Javascript API are different things, while they will often return similar or the same results, they aren't guaranteed to. Please provide a minimal reproducible example that demonstrates your issue, and a link to the Google Map that you are comparing to (rather than a picture of it)geocodezip
@geocodezip Thank you, i have edited the question. The problem is Google maps is using 'Driving + Walking' as modes to reach destination, however in Google Maps JavaScript API, I have only found ways to get directions which uses only single mode, that end up not finding a shorter route which is possible if we drive close to the destination and then walk a bit. How is it possible to use multiple modes while finding directions in Google Maps JavaScript API?GeekForGeek

1 Answers

0
votes

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!