Reputation: 1637
I want to use Google Direction API with only origin and waypoints to get the shortest path without destination point. Is that possible?
Upvotes: 0
Views: 1611
Reputation: 1094
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
}
});
}
Upvotes: -1