gerti
gerti

Reputation: 132

get old position of dragend marker in google maps api v3 (javascript)

hello I searched on the web but I couldn't find the answer so I need your precious help. There is my code:

    for(var i=0; i<10;i++){         
        LatLon=new google.maps.LatLng(39.633336, 22.386423);
        Marker= new google.maps.Marker({position:LatLon,map:MyMap,draggable: true,title:'Mytitle'+i});
        MarkersArray[] = Marker; //keep markers in array because i want to delete them later!!!
        google.maps.event.addListener(Marker,"dragend",function(event){
            var markerNewPosition = this.getPosition();
            alert("old position: **here i want to show the old position** \nnew position: "+markerNewPosition);
        });
    }

until now, when i drag the marker I alert the latitude and longitude of the new position of it. But i want something more! I want to alert the latitude and longitude of the old position. With other words I want in the dragend handler to get the coords of the previous place that marker was and the coords of the new place that marker goes. Dose anybody know a way how to do it? any idea will be appreciate. Thank you in advance!

Upvotes: 3

Views: 4014

Answers (1)

Joseph Erickson
Joseph Erickson

Reputation: 2294

You'll need to grab that with a dragstart event listener. You could store that like so:

var GLOBAL = {};

...

google.maps.event.addListener(Marker,"dragstart",function(event){
    var markerOldPosition = this.getPosition();
    GLOBAL.startDragPosition = markerOldPosition;

});
google.maps.event.addListener(Marker,"dragend",function(event){
    var markerNewPosition = this.getPosition();
    alert("old position: " + GLOBAL.startDragPosition + " \nnew position: "+markerNewPosition);
    delete GLOBAL.startDragPosition;
});

Upvotes: 11

Related Questions