Flyer - get the latitude and longitude of the marker inside the popup

I am using the Leaflet Draw plugin.
My goal is to create markers and show a popup in which I can get the latitude and longitude coordinates.
I manage to get these coordinates using a javascript warning, but I definitely don't know how to put the coordinates in my popup.

Here is a snippet:

map.on('draw:created', function (e) {
        var type = e.layerType,
        layer = e.layer;

        if (type === 'marker') {
            map.on('click', function(e) {
                var lat = e.latlng.lat;
                var lng = e.latlng.lng;
                alert ("Latitude : " + lat + "\nLongitude : " + lng);
        }),

            layer.bindPopup(
            'e.latlng.lat');
        }

        drawnItems.addLayer(layer);
    });

But that will not work. The popup shows "e.latlng.lat", while I want to have the exact value .
Do you have any solutions? Thank.

+4
source share
1 answer
map.on('draw:created', function (e) {
    var type = e.layerType,
        layer = e.layer;

    map.addLayer(layer);

    if (type === 'marker') {    
        layer.bindPopup('LatLng: ' + layer.getLatLng()).openPopup();
    }

});
+8
source

Source: https://habr.com/ru/post/1546049/


All Articles