var directions = [];
function initialize() {
// Create a new map with some default settings
var myLatlng = new google.maps.LatLng(-37.8602828, 145.079616);
var myOptions = {
zoom: 8,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
//click function call calculateAndDisplayRoute to ge the alternative routes
var directionsService = new google.maps.DirectionsService;
document.getElementById('submit').addEventListener('click', function () {
if (directions && directions.length > 0) {
for (var i=0; i<directions.length; i++)
directions[i].setMap(null);
}
directions = [];
calculateAndDisplayRoute(directionsService, map);
});
//this function used to calculate the alternative route.
function calculateAndDisplayRoute(directionsService, map) {
//get the value from start and end input box
var start = document.getElementById('start').value;
var end = document.getElementById('end').value;
//property when dran on the map
var directionsRequest = {
//starting point
origin: start,
//destination
destination: end,
//multiple route
provideRouteAlternatives: true,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(directionsRequest, function (response, status) {
if (status === google.maps.DirectionsStatus.OK) {
//store the multiple routes in respones and display one by one
for (var i = 0, len = response.routes.length; i < len; i++) {
directions.push(new google.maps.DirectionsRenderer({
map: map,
directions: response,
routeIndex: i
}));
}
} else...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.