Google映射apiv3'click'和'dragend'单个监听器

Ryl*_*ton 6 javascript google-maps-api-3

通过点击工作我的代码来检索lat,它工作正常.想知道是否可以在单个事件监听器中组合"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); }
 }
Run Code Online (Sandbox Code Playgroud)

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

Dr.*_*lle 7

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

当您的问题是您不想两次定义回调函数时,您可以使用非匿名函数或为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');} );
Run Code Online (Sandbox Code Playgroud)

与动画相关:
您也可以使用方法setOptions一次设置多个选项,而不是使用marker-methods(setAnimation,setPosition)来设置标记选项:

marker.setOptions({ position  : location,
                    animation : google.maps.Animation.DROP});
Run Code Online (Sandbox Code Playgroud)