markerArray = [];
// Instantiate a directions service.
var directionsService = new google.maps.DirectionsService;
// Create a renderer for directions
var directionsDisplay;
$(document).ready(initialize());
function initialize() {
var bangalore = {
lat: 12.97,
lng: 77.59
};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: bangalore
});
directionsDisplay = new google.maps.DirectionsRenderer({ //bind it to the map.
map: map,
suppressMarkers: true
});
// This event listener calls addMarker() when the map is clicked.
google.maps.event.addListener(map, 'click', function(event) {
addMarker(event.latLng, map);
calculateAndDisplayRoute();
});
//add the first marker
markerArray[0] = new MarkerWithLabel({
position: bangalore,
map: map,
animation: google.maps.Animation.DROP, //just for fun
labelContent: "",
labelClass: "marker-label"
});
}
// Adds a marker to the map.
function addMarker(location, map) {
markerArray[1] = new MarkerWithLabel({
position: location,
map: map,
animation: google.maps.Animation.DROP, //just for fun
labelContent: "",
labelClass: "marker-label"
});
};
function calculateAndDisplayRoute() {
// Retrieve the start and end locations and create a DirectionsRequest using
// WALKING directions.
directionsService.route({
origin: markerArray[0].getPosition(),
destination: markerArray[markerArray.length - 1].getPosition(),
travelMode: google.maps.TravelMode.WALKING
}, function(response, status) {
if (status === google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
route = directionsDisplay.getDirections().routes[0];
markerArray[1].set('labelContent', route.legs[0].distance.value / 1000)
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
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.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
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!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.