3

您好,我在网上搜索但找不到答案,所以我需要您的宝贵帮助。有我的代码:

    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);
        });
    }

直到现在,当我拖动标记时,我会提醒它新位置的纬度和经度。但我想要更多的东西!我想提醒旧位置的经纬度。换句话说,我希望在 dragend 处理程序中获取标记所在位置的坐标和标记所在的新位置的坐标。有人知道怎么做吗?任何想法将不胜感激。先感谢您!

4

1 回答 1

11

您需要使用dragstart事件侦听器来获取它。你可以这样存储:

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;
});
于 2012-12-11T13:16:23.333 回答