How to fire dragend event of a marker in google maps v3?
Asked Answered
D

4

5

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.
});
Drummer answered 26/6, 2010 at 8:15 Comment(0)
E
9

Use event.trigger;

google.maps.event.trigger(markerObject, 'dragend', args);
Ereshkigal answered 26/6, 2010 at 10:49 Comment(1)
Why? The event callback needs to know which marker!Ereshkigal
C
9

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

Chimkent answered 9/1, 2013 at 18:47 Comment(1)
this is not the answer. you add listener instead of execute itConjugal
C
1

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')
});
Chastise answered 20/8, 2012 at 15:31 Comment(1)
triggering means firing the event manually. addListener is used for listening on an event.Drummer
O
1

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
});
Overgrowth answered 4/6, 2016 at 18:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.