An example that calculates the middle of a route
Uses the v3 version of the epoly library of Mike Williams.
var directionDisplay; var directionsService = new google.maps.DirectionsService(); var map; var polyline = null; var infowindow = new google.maps.InfoWindow(); function createMarker(latlng, label, html) { var contentString = '<b>'+label+'</b><br>'+html; var marker = new google.maps.Marker({ position: latlng, map: map, title: label, zIndex: Math.round(latlng.lat()*-100000)<<5 }); marker.myname = label; google.maps.event.addListener(marker, 'click', function() { infowindow.setContent(contentString+"<br>"+marker.getPosition().toUrlValue(6)); infowindow.open(map,marker); }); return marker; } function initialize() { directionsDisplay = new google.maps.DirectionsRenderer({suppressMarkers:true}); var chicago = new google.maps.LatLng(41.850033, -87.6500523); var myOptions = { zoom: 6, mapTypeId: google.maps.MapTypeId.ROADMAP, center: chicago } map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); polyline = new google.maps.Polyline({ path: [], strokeColor: '#FF0000', strokeWeight: 3 }); directionsDisplay.setMap(map); calcRoute(); } function calcRoute() { var start = document.getElementById("start").value; var end = document.getElementById("end").value; var travelMode = google.maps.DirectionsTravelMode.DRIVING var request = { origin: start, destination: end, travelMode: travelMode }; directionsService.route(request, function(response, status) { if (status == google.maps.DirectionsStatus.OK) { polyline.setPath([]); var bounds = new google.maps.LatLngBounds(); startLocation = new Object(); endLocation = new Object(); directionsDisplay.setDirections(response); var route = response.routes[0]; var summaryPanel = document.getElementById("directions_panel"); summaryPanel.innerHTML = "";
source share