Google地图路线规划:
需求:给定的两点之间Google地图路径规划和详情。
代码实现:
1 //map定义省略 2 3 var directionsDisplay = new google.maps.DirectionsRenderer({ 'draggable': true ,suppressInfoWindows:true,suppressMarkers:true}); 4 5 var service; 6 7 //得到规划路线 8 function getRoute(source,destination,map){ 9 var directionsService = new google.maps.DirectionsService(); 10 directionsDisplay.setMap(map); 11 var request = { 12 origin: source, 13 destination: destination, 14 travelMode: google.maps.TravelMode.DRIVING 15 }; 16 directionsService.route(request, function (response, status) { 17 if (status == google.maps.DirectionsStatus.OK) { 18 directionsDisplay.setDirections(response); 19 } 20 }); 21 } 22 23 //得到规划路径的详细些信息 24 function getDetailsRoutes(source,destination,map){ 25 service = new google.maps.DistanceMatrixService(); 26 service.getDistanceMatrix({ 27 origins: [source], 28 destinations: [destination], 29 travelMode: google.maps.TravelMode.DRIVING, 30 unitSystem: google.maps.UnitSystem.METRIC, 31 avoidHighways: false, 32 avoidTolls: false 33 }, function (response, status) { 34 if (status == google.maps.DistanceMatrixStatus.OK && response.rows[0].elements[0].status != "ZERO_RESULTS") { 35 var distance = response.rows[0].elements[0].distance.text; 36 var duration = response.rows[0].elements[0].duration.text; 37 var dvDistance = document.getElementById("siteinfo_modal_label"); 38 dvDistance.innerHTML = ""; 39 dvDistance.innerHTML += "Distance: " + distance + "<br />"; 40 dvDistance.innerHTML += "Duration:" + duration; 41 42 } else { 43 alert("Unable to find the distance via road."); 44 } 45 });