I want to be able to click on two locations on the map and have the recommended cycling route displayed. To do this we need to define two map marker objects to show the start and end of the route and a directionsService object to get the route. Add the following variable declarations before the initialize function.
var directionsService = new google.maps.DirectionsService();
var startMarker, endMarker;
var startMarker, endMarker;
Now the click event handler we wrote in the previous tutorial needs to be extended. The first time we click the startMarker will be null so we will create it at the click location. The second time we click the startMarker will not be null so we will create the endMarker at the click location and then calculate the route. Like geocoding, routing is also asynchronous so we will make the request and tell Google Maps where to send the results. Add the following code to the end of the showaddress function (ideally we would rename it at this point but for simplicity we won't).
if (startMarker != null && endMarker != null)
{ // Clear the existing route, if any
startMarker=null;
endMarker=null;
directionsRenderer.setMap(null);
}
if (startMarker == null)
{ // No markers so create the startMarker
var markerOptions={position:event.latLng, map:map, title:"Start"};
startMarker = new google.maps.Marker(markerOptions);
}
else if (endMarker == null)
{ // Start marker exists so create end marker and request route
var markerOptions={position:event.latLng, map:map, title:"End"};
endMarker = new google.maps.Marker(markerOptions);
var DirectionsRequest = { origin: startMarker.position,
destination: endMarker.position,
travelMode: google.maps.TravelMode.BICYCLING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: true,
avoidTolls: true };
directionsService.route(DirectionsRequest, displaydirections);
}
{ // Clear the existing route, if any
startMarker=null;
endMarker=null;
directionsRenderer.setMap(null);
}
if (startMarker == null)
{ // No markers so create the startMarker
var markerOptions={position:event.latLng, map:map, title:"Start"};
startMarker = new google.maps.Marker(markerOptions);
}
else if (endMarker == null)
{ // Start marker exists so create end marker and request route
var markerOptions={position:event.latLng, map:map, title:"End"};
endMarker = new google.maps.Marker(markerOptions);
var DirectionsRequest = { origin: startMarker.position,
destination: endMarker.position,
travelMode: google.maps.TravelMode.BICYCLING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: true,
avoidTolls: true };
directionsService.route(DirectionsRequest, displaydirections);
}
Now we need to add the routing callback function for Google Maps to pass the results to. It will use a directions rendering object to draw the route. Add the following declaration below the directionsService declaration.
var directionsRenderer = new google.maps.DirectionsRenderer();
Add the following function below the showaddress function.
function displaydirections(result, status)
{ if (status == google.maps.DirectionsStatus.OK)
{ startMarker.setMap(null); // the renderer displays its own markers
endMarker.setMap(null);
directionsRenderer.setDirections(result);
directionsRenderer.setMap(map);
}
else
alert("Plotting route:" + status);
}
{ if (status == google.maps.DirectionsStatus.OK)
{ startMarker.setMap(null); // the renderer displays its own markers
endMarker.setMap(null);
directionsRenderer.setDirections(result);
directionsRenderer.setMap(map);
}
else
alert("Plotting route:" + status);
}
Run the page and click on two locations. The recommended route between those locations is shown. Note how the map redisplays with the cycling layer displayed because we specified TravelMode.BYCYCLING. It would be nice if the map originally displayed in cycling mode. Bicycling mode is added as a layer. Add the following code to the end of the initialize function.
var bikeLayer = new google.maps.BicyclingLayer();
bikeLayer.setMap(map);
bikeLayer.setMap(map);
No comments:
Post a Comment