2011-12-17 90 views
0

我想要像我的指定位置谷歌地圖方向谷歌地圖路線。 現在谷歌地圖內部計算最短路徑和顯示路線,但我想給位置的路徑,以便路線必須通過指定的位置。通過指定地點的谷歌地圖路線?

任何想法,帶路標的GDirections如何工作?

回答

1

,你必須使用waypoints

working sample

var directionsDisplay; 
var directionsService = new google.maps.DirectionsService(); 
var map; 

function initialize() { 
    directionsDisplay = new google.maps.DirectionsRenderer(); 
    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); 
    directionsDisplay.setMap(map); 
} 

function calcRoute() { 
    var start = document.getElementById("start").value; 
    var end = document.getElementById("end").value; 
    var waypts = []; 
    var checkboxArray = document.getElementById("waypoints"); 
    for (var i = 0; i < checkboxArray.length; i++) { 
    if (checkboxArray.options[i].selected == true) { 
     waypts.push({ 
      location:checkboxArray[i].value, 
      stopover:true 
     }); 
    } 
    } 

    var request = { 
     origin: start, 
     destination: end, 
     waypoints: waypts, 
     optimizeWaypoints: true, 
     travelMode: google.maps.TravelMode.DRIVING 
    }; 
    directionsService.route(request, function(response, status) { 
    if (status == google.maps.DirectionsStatus.OK) { 
     directionsDisplay.setDirections(response); 
     var route = response.routes[0]; 
     var summaryPanel = document.getElementById("directions_panel"); 
     summaryPanel.innerHTML = ""; 
     // For each route, display summary information. 
     for (var i = 0; i < route.legs.length; i++) { 
     var routeSegment = i+1; 
     summaryPanel.innerHTML += "<b>Route Segment: " + routeSegment + "</b><br />"; 
     summaryPanel.innerHTML += route.legs[i].start_address + " to "; 
     summaryPanel.innerHTML += route.legs[i].end_address + "<br />"; 
     summaryPanel.innerHTML += route.legs[i].distance.text + "<br /><br />"; 
     } 
    } 
    }); 
}