-1

直到現在......我一直在一個方向服務對象中傳遞一個源和一個目標值,這將在源和目的地之間繪製一條路線。但這裏我收到一個經緯度數組,我需要將它傳遞給方向服務對象如何在方向服務對象內傳遞緯度和經度數組?

例如:我有四個地方像紐約,華盛頓,曼哈頓, 加州。所以我應該能夠從紐約到 加利福尼亞州的路線從華盛頓和曼哈頓通過路線和 只有兩個標記。紐約和B的標記 加利福尼亞州。

我嘗試過使用方法點。但在我的例子中,它使用地名而不是經度和緯度。此外,它爲它訪問的每個地方創造了一個標記。

我的要求是傳遞數組中的經度和緯度,並使用directionservice繪製 路線。

請找到下面的代碼。

<!DOCTYPE html> 
<html> 
<head> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
<meta charset="utf-8"> 
<title>Chart</title> 
<style> 
html,body,#map-canvas { 
    height: 100%; 
    margin: 0px; 
    padding: 0px 
} 

#panel { 
    position: absolute; 
    top: 5px; 
    left: 50%; 
    margin-left: -180px; 
    z-index: 5; 
    background-color: #fff; 
    padding: 5px; 
    border: 1px solid #999; 
} 
</style> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 



<script> 

var directionsDisplay; var directionsService = new google.maps.DirectionsService(); 
var map; 
function initialize() 
{ 
    directionsDisplay = new google.maps.DirectionsRenderer(); 
    var delhi = new google.maps.LatLng(28.6168, 77.2434); 
    var mapOptions = 
     {  
      zoom: 6,  
      mapTypeId: google.maps.MapTypeId.ROADMAP,  
      center: delhi 
     } 
     map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 

     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, 
       provideRouteAlternatives:true, 
      // alternatives:true,  
       optimizeWaypoints: true,  
       travelMode: google.maps.TravelMode.DRIVING 
      }; 

     var polylineOptions = new google.maps.Polyline({ 
      strokeColor: '#000000', 
      strokeOpacity: 1.0, 
      strokeWeight: 2 
     }); 






     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>';  
          }  
} }); 
} 


google.maps.event.addDomListener(window, 'load', initialize);  </script> 
</head> 
<body> 
<div id="map-canvas" style="float: left; width: 70%; height: 100%;"></div> 
<div id="control_panel" 
    style="float: right; width: 30%; text-align: left; padding-top: 20px"> 
<div style="margin: 20px; border-width: 2px;"><b>Loco Journey Start Station:</b> <select 
    id="start"> 
    <option value="Tughlakabad">Tughlakabad</option> 
    <option value="Lucknow">Lucknow</option> 
    <option value="Firozpur">Firozpur</option> 
    <option value="Ghaziabad">Ghaziabad</option> 
</select> <br> 
<b>Journey:</b> <br> 
<i>(Ctrl-Click for multiple selection)</i> <br> 
<select multiple id="waypoints"> 
    <option value="Bhopal">Bhopal</input> 
    <option value="Raipur">Raipur</input> 
    <option value="Farukkhabad">Farukkhabad</input> 
    <option value="Jhansi">Jhansi</input> 
</select> <br> 
<b>Loco Journey End Station:</b> <select id="end"> 
    <option value="Lucknow">Lucknow</option> 
    <option value="Firozpur">Firozpur</option> 
    <option value="Ghaziabad">Ghaziabad</option> 
    <option value="Tughlakabad">Tughlakabad</option> 
</select> <br> 
<input type="submit" onclick="calcRoute();"></div> 
<div id="directions_panel" 
    style="margin: 20px; background-color: #FFEE77;"></div> 
</div> 
</body> 
</html> 

回答

0

DirectionsRequest

它可以是一個字符串(一個地址)或google.maps.LatLng object (for coordinates)

origin  | LatLng|string | Location of origin. This can be specified as either a string to be geocoded or a LatLng. Required. 
destination | LatLng|string | Location of destination. This can be specified as either a string to be geocoded or a LatLng. Required. 

Waypoints採取一個布爾「停留」標誌,其確定其中與否他們得到的標記。將其設置爲false。它也可以在位置屬性中使用字符串(對於地址)或google.maps.LatLng object (for coordinates)

stopover | boolean | If true, indicates that this waypoint is a stop between the origin and destination. This has the effect of splitting the route into two. This value is true by default. Optional.