//You can calculate directions (using a variety of methods of transportation) by using the DirectionsService object.
var directionsService = new google.maps.DirectionsService();
//Define a variable with all map points.
var _mapPoints = new Array();
//Define a DirectionsRenderer variable.
var _directionsRenderer = '';
//InitializeMap() function is used to initialize google map on page load.
function InitializeMap() {
//DirectionsRenderer() is a used to render the direction
_directionsRenderer = new google.maps.DirectionsRenderer();
//Set the your own options for map.
var myOptions = {
zoom: 6,
center: new google.maps.LatLng(21.7679, 78.8718),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
//Define the map.
var map = new google.maps.Map(document.getElementById("dvMap"), myOptions);
//Set the map for directionsRenderer
_directionsRenderer.setMap(map);
//Set different options for DirectionsRenderer mehtods.
//draggable option will used to drag the route.
_directionsRenderer.setOptions({
draggable: true
});
//Add the doubel click event to map.
google.maps.event.addListener(map, "dblclick", function(event) {
//Check if Avg Speed value is enter.
if ($("#txtAvgSpeed").val() == '') {
alert("Please enter the Average Speed (km/hr).");
$("#txtAvgSpeed").focus();
return false;
}
var _currentPoints = event.latLng;
_mapPoints.push(_currentPoints);
getRoutePointsAndWaypoints();
});
//Add an event to route direction. This will fire when the direction is changed.
google.maps.event.addListener(_directionsRenderer, 'directions_changed', function() {
computeTotalDistanceforRoute(_directionsRenderer.directions);
});
}
//getRoutePointsAndWaypoints() will help you to pass points and waypoints to drawRoute() function
function getRoutePointsAndWaypoints() {
//Define a variable for waypoints.
var _waypoints = new Array();
if (_mapPoints.length > 2) //Waypoints will be come.
...
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.