Javascript 两次单击并通过不同的标记获取两个坐标

Javascript 两次单击并通过不同的标记获取两个坐标,javascript,google-maps-api-3,Javascript,Google Maps Api 3,我有一个功能,我可以给坐标输入点击地图 我想通过在地图上点击两次,就可以得到地图上两点的两个坐标,从而得到它们之间的方向 我该怎么做 var pointa; var pointb; google.maps.event.addListener(map,'click',function(event){ var point=new google.maps.LatLng(event.latLng.lat(),event.latLng.lng());

我有一个功能,我可以给坐标输入点击地图

我想通过在地图上点击两次,就可以得到地图上两点的两个坐标,从而得到它们之间的方向

我该怎么做

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

当他们单击时,您已经在创建标记。通过查看
点a
,您可以很容易地判断他们何时再次单击<代码>点A将以值
未定义
开始。该值为“falsey”,但当您在
pointa
中存储谷歌地图
标记
引用时,该值不再为falsey。因此,您可以使用“
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.)
    }
});
可能的重复可能的重复