How to fire dragend event of a marker in google maps v3?
-
30-09-2019 - |
Question
I want to fire dragend event of a marker in another event say click event on the map. how can I do that?
google.maps.event.addListener(map,'click',function(pt){ posSelectMarker.setPosition(pt.latLng); //Here I want to fire dragend event. });
Solution
Use event.trigger
;
google.maps.event.trigger(markerObject, 'dragend', args);
OTHER TIPS
This is a bit more complete:
theListener = google.maps.event.addListener(posSelectMarker,'dragend',function(event){
console.log(event.latLng);
});
Note that you can get at the object with the event param
Should be:
google.maps.event.addListener
instead of:
google.maps.event.trigger
Quick example:
google.maps.event.addListener(marker_var_name, 'dragend', function(){
alert('drag ended')
});
If you have the marker object, you could call addListener
directly to add a dragend
event.
var marker = new google.maps.Marker({
...
)};
marker.addListener('dragend', function() {
// do something
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow