最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google maps apiv3 'click' and 'dragend' single listener - Stack Overflow

programmeradmin1浏览0评论

working on my code to retrieve lat,long by clicking and it works fine. wondering if its possible to bine 'click' and 'dragend' in a single event listener if not. Would appreciate whats a proper alternative.

This is what I've been working on.

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

I tried doing this google.maps.event.addListener(map,'click','dragend', function(event) but to no avail. also if anyone has an idea to modify this marker.setPosition(location); to have animation: google.maps.Animation.DROP . . thanks in advance.

working on my code to retrieve lat,long by clicking and it works fine. wondering if its possible to bine 'click' and 'dragend' in a single event listener if not. Would appreciate whats a proper alternative.

This is what I've been working on.

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

I tried doing this google.maps.event.addListener(map,'click','dragend', function(event) but to no avail. also if anyone has an idea to modify this marker.setPosition(location); to have animation: google.maps.Animation.DROP . . thanks in advance.

Share asked Jan 13, 2013 at 6:43 Ryler SextonRyler Sexton 1533 silver badges10 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

There is no built-in method to apply a listener for multiple events.

When your problem is that you don't want to define the callback-function twice, you may either use a non-anonymous function or define the listener for 1 event and trigger the event when the other event fires:

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

Related to the animation:
Instead of using the marker-methods(setAnimation,setPosition) to set marker-options, you also may use the method setOptions to set multiple options at once:

marker.setOptions({ position  : location,
                    animation : google.maps.Animation.DROP});
发布评论

评论列表(0)

  1. 暂无评论