I want to use Google Direction API with only origin and waypoints to get the shortest path without destination point. Is that possible?
Asked
Active
Viewed 1,458 times
1 Answers
-1
I am afraid you can not, destination is a required parameter in all Directions API requests, but you can do a request without waypoints.
If you want the shortest path you just need to iterate through the response to get it.
here is an example how to do it: https://jsbin.com/nihihis/1/edit?html,js,output
in this I iterate the response and show the shortest path as green and the longest as red.
var request = {
origin: start,
destination: end,
travelMode: "DRIVING",
provideRouteAlternatives: true
};
directionsService.route(request, function(response, status) {
console.log('init');
console.log('status ' + status);
if (status == 'OK') {
console.log('results', response.routes);
//
var routesSteps = [];
var routes = response.routes;
var colors = ['red', 'black', 'green'];
for (var i = 0; i < routes.length; i++) {
if(i==0) {
new google.maps.DirectionsRenderer({
map: map,
directions: response,
routeIndex: i,
polylineOptions: {
strokeColor: colors[2],
strokeWeight: 4,
strokeOpacity: .3
}
});
} else if(i== routes.length - 1) {
new google.maps.DirectionsRenderer({
map: map,
directions: response,
routeIndex: i,
polylineOptions: {
strokeColor: colors[0],
strokeWeight: 4,
strokeOpacity: .3
}
});
} else {
new google.maps.DirectionsRenderer({
map: map,
directions: response,
routeIndex: i,
polylineOptions: {
strokeColor: colors[1],
strokeWeight: 4,
strokeOpacity: .3
}
});
}

betofarina
- 1,066
- 7
- 9
-
possible duplicate of [Use one waypoint as destination in Gmaps Api](https://stackoverflow.com/questions/33858089/use-one-waypoint-as-destination-in-gmaps-api) – geocodezip Jan 26 '18 at 02:52