【问题标题】:get the details of particular dragged marker in a loop在循环中获取特定拖动标记的详细信息
【发布时间】:2012-11-06 23:03:40
【问题描述】:

您好,我正在使用下面的代码来填充标记,并且我使用了一个函数来实现标记的拖动,并且在 dragend 上我得到了新位置的 lat,lng.... ..

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"]);

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

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

我刚开始学习编程....请告诉我如何处理这种情况........谢谢

【问题讨论】:

    标签: javascript


    【解决方案1】:

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

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多