6

处理我的代码以通过单击检索经纬度,它工作正常。想知道是否可以将“click”和“dragend”组合在一个事件侦听器中,如果没有的话。希望有什么合适的选择。

这就是我一直在做的事情。

google.maps.event.addListener(map,'click', function(event) {
    document.getElementById("latbox").value = event.latLng.lat();
    document.getElementById("lngbox").value = event.latLng.lng();
    addMarker(event.latLng);
  });
}

 function addMarker(location) {

if (!marker) {

    marker = new google.maps.Marker({
    position: location,
    map: map,
    draggable:true,
    animation: google.maps.Animation.DROP
    });
}
// Otherwise, simply update its location on the map.
else { marker.setPosition(location); }
 }

我尝试这样做,google.maps.event.addListener(map,'click','dragend', function(event)但无济于事。另外,如果有人有想法将其修改marker.setPosition(location);animation: google.maps.Animation.DROP. . 提前致谢。

4

1 回答 1

9

没有内置方法可以为多个事件应用侦听器。

当您的问题是您不想定义回调函数两次时,您可以使用非匿名函数或为 1 个事件定义侦听器并在另一个事件触发时触发该事件:

google.maps.event.addListener( map, 'click',
                              function(e){ alert('clicked or dragged');} );
google.maps.event.addListener( map, 'dragend', function(){
                              google.maps.event.trigger(this, 'click');} );

动画相关:
除了使用marker-methods(setAnimation,setPosition)来设置marker-options,你也可以使用setOptions方法同时设置多个选项:

marker.setOptions({ position  : location,
                    animation : google.maps.Animation.DROP});
于 2013-01-13T14:15:38.220 回答