Skip to content

Instantly share code, notes, and snippets.

@lances101
Created February 8, 2016 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lances101/c3f65e60e0c1dbe7729f to your computer and use it in GitHub Desktop.
Save lances101/c3f65e60e0c1dbe7729f to your computer and use it in GitHub Desktop.
Directions services for Gmaps
var directionsService = new google.maps.DirectionsService();
var renderOptions = { draggable: true };
var directionDisplay = new google.maps.DirectionsRenderer(renderOptions);
//set the directions display service to the map
directionDisplay.setMap(map);
//set the directions display panel
//panel is usually just and empty div.
//This is where the turn by turn directions appear.
directionDisplay.setPanel(directionsPanel);
//build the waypoints
//free api allows a max of 9 total stops including the start and end address
//premier allows a total of 25 stops.
var items = ["address 1", "address 2", "address 3"];
var waypoints = [];
for (var i = 0; i < items.length; i++) {
var address = items[i];
if (address !== "") {
waypoints.push({
location: address,
stopover: true
});
}
}
//set the starting address and destination address
var originAddress = "starting address";
var destinationAddress = "destination address";
//build directions request
var request = {
origin: originAddress,
destination: destinationAddress,
waypoints: waypoints, //an array of waypoints
optimizeWaypoints: true, //set to true if you want google to determine the shortest route or false to use the order specified.
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
//get the route from the directions service
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionDisplay.setDirections(response);
}
else {
//handle error
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment