2013-08-17 42 views
0

我創建了一個代碼,可以在地圖上繪製繪圖方向,但使用多段線,因爲使用航點我可以看到八個點。谷歌地圖方向

首先我有一個函數nArray()返回我想在這個控制檯中顯示的一個值設置爲一個點

Array[5] 
    0: Object 
    1: Object 
     DISTANCE_FROM_PREVIOUS_OBJECT_LOCATION: 2.087970147789207 
     lat: "48.866588" 
     leftPosition: 183 
     lng: "2.309037999999987" 
     topPosition: 57 
      __proto__: Object 
    2: Object 
    3: Object 
    4: Object 
    length: 5 
    __proto__: Array[0] 

現在我想對所有對象(緯度,經度):

<!DOCTYPE html> 
<html> 
    <head> 
     <script src="http://maps.google.com/maps/api/js?sensor=false&libraries=places" type="text/javascript"></script> 
     <script> 
     var map = null; 
     var directionService = null; 
     var directionsRenderer = null; 

     var bigArray = nArray(); 

     function initialize() { 
      var mapOptions = { 
      center: new google.maps.LatLng(35.75936182169444, -90.35907120698232), 
      mapTypeId: google.maps.MapTypeId.ROADMAP, 
      zoom: 4 
      }; 
      map = new google.maps.Map(document.getElementById("map"), mapOptions); 
      directionService = new google.maps.DirectionsService(); 
      directionsRenderer = new google.maps.DirectionsRenderer({ map: map });  
     } 

     function drawDirections(myPath) { 
      var myLine = new google.maps.Polyline({ 
       path: myPath, 
       strokeColor: '#ff0000', 
       strokeOpacity: 1.0, 
       strokeWeight: 2 
      }); 
      myLine.setMap(map); 
     } 

     function drawRoutes() { 
      var i; 
      var npts = bigArray.length - 1; 

      for (i = 0; i < npts; i++) { 
       var marker = new google.maps.Marker({ 
        map: map, 
        position: new google.maps.LatLng(bigArray[i].lat, bigArray[i].lng) 
       }); 
       var request = { 
        origin: new google.maps.LatLng(bigArray[i].lat, bigArray[i].lng), 
        destination: new google.maps.LatLng(bigArray[i + 1].lat, bigArray[i + 1].lng), 
        travelMode: google.maps.DirectionsTravelMode.DRIVING 
       } 

       directionService.route(request, function(result, status) { 
        //route = result.routes[0]; 
        if (status == google.maps.DirectionsStatus.OK) { 
         drawDirections(result.routes[0].overview_path); 
        } 
       }); 
      } 

      var marker = new google.maps.Marker({ 
       map: map, 
       position: new google.maps.LatLng(bigArray[i].lat, bigArray[i].lng) 
      }); 
     } 
     </script> 
    </head> 
    <body onload="initialize();"> 
     <div id="map" style="width: 500px; height: 400px;"></div> 
     <input type="button" onclick="drawRoutes()" value="Draw Routes" /> 
    </body> 
</html> 

如何從nArray獲取每個對象的經度和緯度?

回答

0

您目前已經獲得的緯度和經度在循環中的每個對象,但也有2個問題:

  1. 得到相同的結果,當您使用的航點中的DirectionsRenderer,你必須使用結果(例如result.routes[0].legs[0].start_location)來設置標記的位置,否則標記將不會被放置在路線上

  2. 您應該忘記這種方法超出路點限制,每秒有10個請求的限制,您很快會得到響應「OVER_QUERY_LIMIT」

+0

那麼,你有什麼建議?使用航點? –

+0

這將是最好的。但是,如果您需要8個以上的航點,則無法使用(沒有商業許可證),可能的選項有:在請求之間設置一個延遲(以便每秒不會達到極限),當數組包含10個以上點(否則你不需要解決方法,航點將工作),將數組拆分爲10個位置,並請求這些點的路線...(例如,如果有15個位置,則需要2個請求,而不是14你的當前策略) –

+0

好吧,但是有一些例子,你能告訴我一個jsfiddle嗎? –