1

嗨,我正在使用下面的代码来填充标记,并且我使用了一个函数来实现标记的拖动,并且在dragend上我得到了新位置的纬度、经度......

for (var key in data) {
    if ((data[key]["latitude"] != null) & (data[key]["longitude"] != null)){
       //populating the markers
       if (data.hasOwnProperty(key) ) {
            m = L.marker([data[key]["latitude"], data[key]["longitude"]], {
                    icon: compIcon,title:"Click and Hold Marker to drag",draggble:true
                }).bindPopup(data[key]["name"]).addTo(comp_markers);

            m.on('dragend', function(e) {

                console.log("name of the marker :"+data[key]["name"]);

                console.log("please show me changed lat & lon:"+this.getLatLng());

            });

        }

    }
 }

我的问题是:如何获取我拖动的标记的名称,但下面的行给出了数组中最后一个标记的名称.....

console.log("name of the marker :"+data[key]["name"]);

我尝试了这个关键字,期望它指向用户拖动的确切标记......

console.log("name of the marker :"+this.data[key]["name"]);

我刚开始学习编程....请让我知道如何处理这种情况........谢谢

4

1 回答 1

0

使用另一个外部函数可能更容易(也更易读):

for (var key in data) {
    if ((data[key]["latitude"] != null) & (data[key]["longitude"] != null)){
       //populating the markers
       if (data.hasOwnProperty(key) ) {
            m = L.marker([data[key]["latitude"], data[key]["longitude"]], {
                    icon: compIcon,title:"Click and Hold Marker to drag",draggble:true
                }).bindPopup(data[key]["name"]).addTo(comp_markers);

            m.on('dragend', getDragEndFunction(data, key));
        }

    }
}

function getDragEndFunction(data, key)
{
    return function(e) {
        console.log("name of the marker :"+data[key]["name"]);
        console.log("please show me changed lat & lon:"+this.getLatLng());
    }
}
于 2012-11-06T15:03:40.073 回答