2013-10-12 50 views
0

我有一個功能,我可以通過點擊地圖給輸入座標。兩個點擊並通過不同的標記獲得兩個座標

我想要在地圖上得到兩點的兩個座標,通過雙擊地圖來獲得他們之間的方向。

我該怎麼做?

var pointa; 
     var pointb; 
    google.maps.event.addListener(map,'click',function(event){ 
      var point=new google.maps.LatLng(event.latLng.lat(),event.latLng.lng()); 
      document.path.lat1.value=event.latLng.lat(); 
      document.path.lon1.value=event.latLng.lng(); 

      pointa=new google.maps.Marker({ 
       map: map, 
       position: point, 
       icon:'http://google.com/mapfiles/kml/paddle/A.png ', 
       draggable:true 
      }); 
    }); 
+0

可能重複(http://stackoverflow.com/questions/ [得到兩個點通過點擊地圖座標] 19327605 /獲取兩點座標點的地圖) – geocodezip

+0

[數組在Google Maps v3上創建多條路線]的可能重複(http://stackoverflow.com/questions/11778201/array- to-create-multiple-routes-on-google-maps-v3) – geocodezip

回答

0

您在點擊時已經創建了一個標記。您可以通過查看pointa來輕鬆判斷他們第二次點擊的時間。以pointa開頭的值爲undefined。該值爲「虛假」,但是當您在pointa中存儲Google Maps Marker參考時,它不再是虛僞的。所以,你可以使用「if (!pointa)」要知道,你正在處理的第一個點擊:

var pointa; 
var pointb; 
google.maps.event.addListener(map, 'click', function (event) { 
    var point = new google.maps.LatLng(event.latLng.lat(), event.latLng.lng()); 
    document.path.lat1.value = event.latLng.lat(); 
    document.path.lon1.value = event.latLng.lng(); 

    if (!pointa) { 
     // This is the first click 
     pointa = new google.maps.Marker({ 
      map: map, 
      position: point, 
      icon: 'http://google.com/mapfiles/kml/paddle/A.png ', 
      draggable: true 
     }); 
    } 
    else { 
     // This is a subsequent click (the second, third, etc.) 
    } 
}); 
+0

感謝您的幫助:-) –

相關問題