2013-10-19 79 views
0

運行以下代碼時出現兩個錯誤: 1. Uncaught InvalidValueError:setPosition的無效參數:[object Object] 2.未捕獲的TypeError:無法調用方法'contains'未定義無法使用Google Maps API v3計算道路距離

編輯:我可以使用相同的標記標籤創建多個標記,如下所示?

基本上試圖標記添加到一組經緯度並計算距離一個共同點:

<script type="text/javascript"> 
     var map; 
     var mapOptions; 
     var lat = [33, 34, 35, 36]; 
     var lon = [-84, -86, -89, -100]; 
     var src = [30, -90]; 
     var dist = []; 
     var marker; 
     var directionsService = new google.maps.DirectionsService(); 
     var directionsRequest; 
     var directionsRenderer; 
     var markerPoints = [{"location": new google.maps.LatLng(lat[0], lon[0])}, 
          {"location": new google.maps.LatLng(lat[1], lon[1])}, 
          {"location": new google.maps.LatLng(lat[2], lon[2])}, 
          {"location": new google.maps.LatLng(lat[3], lon[3])}]; 

      function initialize() { 
      directionsRenderer = new google.maps.DirectionsRenderer(); 
      mapOptions = { 
       center: new google.maps.LatLng(33.75, -84.39), 
       mapTypeId: google.maps.MapTypeId.ROADMAP 
      }; 
      map = new google.maps.Map(document.getElementById("map-canvas"), 
        mapOptions); 
      directionsRenderer.setMap(map); 

      marker = new google.maps.Marker({ 
       position: new google.maps.LatLng(src[0], src[1]), 
       //icon: 'http://maps.google.com/mapfiles/marker.png', 
       map: map 
       }); 

      for(var iter = 0; iter < lat.length; iter++) 
      { 
       placeMarker(iter); 
       getDistances(iter); 
      } 
      } 

      function placeMarker(iter) 
      { 
       marker = new google.maps.Marker({ 
       position: markerPoints[iter], 
       //icon: 'http://maps.google.com/mapfiles/marker_green.png', 
       map: map 
       }); 
      } 

      function getDistances(iter) 
      { 
       directionsRequest = { 
        origin: new google.maps.LatLng(src[0], src[1]), 
        destination: markerPoints[iter], 
        travelMode: google.maps.TravelMode.DRIVING, 
       }; 

       directionsService.route(directionsRequest, function(response, status){ 
        if(status == google.maps.DirectionsStatus.OK) 
         { 
          dist.push(response.routes[0].legs[0].distance); 
         } 
        else 
         { 
         alert("Impossible"); 
         } 
       }); 
      } 

     google.maps.event.addDomListener(window, 'load', initialize); 
     </script> 
+1

檢查此鏈接http://christianvarga.com/2010/12/how-to-calculate-driving-distance-between-2-locations-with-google-maps-api/ – shivam

+0

@magExp很好找。我其實不想繪製方向,我只是想把標記放在地圖上。 – codepk

回答

1

在這裏你去...

問題#1:未捕獲InvalidValueError:無效參數setPosition:[object Object]

您的markerPoints數組存在問題。取而代之的

var markerPoints = [{"location": new google.maps.LatLng(lat[0], lon[0])}, 
        {"location": new google.maps.LatLng(lat[1], lon[1])}, 
        {"location": new google.maps.LatLng(lat[2], lon[2])}, 
        {"location": new google.maps.LatLng(lat[3], lon[3])}]; 

應該是這樣..

var markerPoints = [new google.maps.LatLng(lat[0], lon[0]), 
        new google.maps.LatLng(lat[1], lon[1]), 
        new google.maps.LatLng(lat[2], lon[2]), 
        new google.maps.LatLng(lat[3], lon[3])]; 

問題2:遺漏的類型錯誤:無法調用方法 '包含' 未定義:

如果您使用的是實驗api版本(v = 3.exp)for map api,那麼你會得到這個問題。你也應該使用任何發行版本。你可以通過訪問Google Map API Version information section

找到不同版本的細節。希望這會對你有所幫助。

+0

太棒了!我改變了他們倆。現在距離信息已填充,但地圖仍然無法加載。有任何想法嗎? – codepk

+1

乾杯,我設法修復它。我沒有在mapOptions中給出縮放級別(我的錯誤)。 – codepk