Leaflet - get latitude and longitude of a marker inside a pop-up

21,272

Solution 1

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

});

Solution 2

It return lat & lng on the map when mouse clicked

map.on('click', function(e){
  var lt = String(e.latlng.lat),
  lg = String(e.latlng.lng);
  var popup = L.popup()
    .setLatLng(e.latlng)
    .setContent(lt + " " + lg)
    .openOn(map);
});
Share:
21,272
Julien
Author by

Julien

Updated on July 09, 2022

Comments

  • Julien
    Julien almost 2 years

    I use the Leaflet Draw plugin.
    My goal is to create markers and show a pop up in which I can get latitude and longitude coordinates.
    I manage to get these coordinates with a javascript alert but I definitely don't know how to put the coordinates into my pop up.

    Here is the 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 it does not work. The pop up shows "e.latlng.lat" whereas I'd want to have the exact value.
    Have you any solutions ? Thanks.