kofhearts
kofhearts

Reputation: 3774

how to access a marker in an google maps event handler?

for(; j<cos[i].length; j++) {
    var markerLatLng = new google.maps.LatLng(cos[i][j].lat, cos[i][j].lng);

    // Place a draggable marker on the map
    var marker = new google.maps.Marker({
        position: markerLatLng,
        map: map,
        draggable: true,
        title: "Drag me!"
    });

    google.maps.event.addListener(marker,  'rightclick',  function(mouseEvent) { 
        alert(marker.getPosition().lat());
    });
}           

The page is alerting the position of the last marker in the list or the lastly created marker. The goal is to display position of each individual marker when right click on that marker is clicked but right clicking is currently only displaying the position of the last marker. Why is the function for event handler only seeing the last marker?

I appreciate any help! Thanks!

Upvotes: 0

Views: 89

Answers (1)

Marat Safin
Marat Safin

Reputation: 1909

Try this keyword:

google.maps.event.addListener(marker,  'rightclick',  function() { 
    alert(this.getPosition().lat());
});

Upvotes: 1

Related Questions