Google Maps API v3 Read draggable markers new positions

I have several tokens retrieved from mySQL database with php, generating the following code for each token:

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 problems:

1) the dragend fire event without dragging, when the markers are first created, doesn’t really matter much, but ...

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

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

+4
source share
1 answer

When addListener is called, the third parameter must be a function. You call the result of the function so as not to work. Try this as follows:

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

For more information, check out the API documentation for closing when listening for events .

+7
source

Source: https://habr.com/ru/post/1398612/


All Articles