1

I am using this code to draw points on the map:

coordsArray.push(lat);
coordsArray.push(lng);
marker = L.marker(coordsArray, {
    icon: myIcon,
    clickable: true,
    draggable: 'false',
    title: layerStringToJson.properties.id
}).bindPopup(coordinates).addTo(map).on('click', onClick);

And I have this method to get individual marker properties:

function onClick(e) {
      //marker=layerStringToJson.properties;
      console.log(marker._latlng)
  }

But when I click on the markers, I always have the same result, always the same coordinates instead of the coordinates of every single marker.

Anyway the pop up shows the correct coordinates.

How can I get the individual properties of each marker?

1

1 Answer 1

1

2 options, for a single marker:

var lat = 43;
var lng = -76;

 var marker = L.marker([lat,lng]).bindPopup(lat.toString()+", "+lng.toString()).addTo(map);
 marker.addTo(map);

For multiple markers use a feature group:

var myMarkers = L.featureGroup();

    // create loop here to add each marker to the featuregroup
    var marker = L.marker([lat,lng]).bindPopup(lat.toString()+", "+lng.toString()).addTo(map);
    //add the marker to the featuregroup
    myMarkers.addLayer(marker);
    //end loop

myMarkers.addTo(map);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.