1

Hi there i am using below code to populate the markers and i used a function to achieve the dragging of the marker and on dragend i am getting the lat,lng of new location......

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

            });

        }

    }
 }

My Question is : How to get the name of marker that I dragged but below line gives me name of last marker in the array........

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

I tried this keyword expecting it will point to exact marker that user dragging....

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

I just started learning programming.... Please let me know how ot handle such kind of sutiations........ Thank You

troy
  • 997
  • 4
  • 13
  • 27

1 Answers1

0

It might be easier (and more readable) to use another external function:

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());
    }
}
marchaos
  • 3,316
  • 4
  • 26
  • 35
  • There are other ways. Might want to take a look at http://stackoverflow.com/questions/2274695/new-function-with-lower-case-f-in-javascript – marchaos Nov 07 '12 at 11:52