Skip to content Skip to sidebar Skip to footer

How To Display Route Between Two Makers In Map Using Javascript

I have facing problem while creating route map with multiple Latitude and Longitude , I tried lot of ways for creating route map but I am getting marker places only, it is not disp

Solution 1:

use Directions Waypoints

Reference https://developers.google.com/maps/documentation/javascript/examples/directions-waypoints

var waypts = [];

for (i = 0; i < locations.length; i++) {    
     waypts.push({
        location: new google.maps.LatLng(locations[i].address.lat, locations[i].address.lng),
        stopover: true
     });
}

directionsService.route({
  origin: new google.maps.LatLng(locations[0].address.lat, locations[0].address.lng),
  destination: new google.maps.LatLng(locations[locations.length - 1].address.lat, locations[locations.length - 1].address.lng),
  waypoints: waypts,
  optimizeWaypoints: true,
  travelMode: 'DRIVING'
}, function(response, status) {
  if (status === 'OK') {
    directionsDisplay.setDirections(response);
    var route = response.routes[0];
  } else {
    window.alert('Directions request failed due to ' + status);
  }
});

Post a Comment for "How To Display Route Between Two Makers In Map Using Javascript"