user1235693
user1235693

Reputation: 41

Google Maps API v3 Read draggable markers new position

I have multiple markers fetched from a mySQL database with php, generating following code for each marker:

var marker28= new google.maps.Marker({position:new google.maps.LatLng(64.2,11.4),map: map,draggable: true,icon: 'slayer.png',title: '#28 slayer (64.2, 11.4)'});
google.maps.event.addListener(marker28, 'dragend', markerMoved(marker28));

var marker25

.... etc

Then I have the following function:

function markerMoved(movedMarker){
     movedMarker.title = movedMarker.position;
}

I have the following 2 issues:

1) the event "dragend" fire without dragging, when the markers are first created, really no big deal, but...

2) the new position is not updated, the title is set to the first initial position no matter where I drag the markers. I tried getPosition() method with same result.

The idea is to write the new positions back to the mySql database from this function, via a (async) GET request to a file track.php

Upvotes: 4

Views: 6895

Answers (1)

Mark
Mark

Reputation: 5609

When you call addListener, the third parameter needs to be a function. You are calling with the result of a function, so that's not going to work. Try it like this:

google.maps.event.addListener(marker28, 'dragend', function() { markerMoved(marker28); } );

For more info, check the API documentation for using closures in event listeners.

Upvotes: 7

Related Questions