Leaflet - 在弹出窗口中获取标记的纬度和经度

2022-01-12 00:00:00 leaflet popup javascript

我使用 Leaflet Draw 插件.
我的目标是创建标记并显示一个弹出窗口,我可以在其中获取纬度和经度坐标.
我设法通过 javascript 警报获取这些坐标,但我绝对不知道如何将坐标放入我的弹出窗口中.

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.

这是片段:

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 + "
Longitude : " + lng);
        }),

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

        drawnItems.addLayer(layer);
    });

但它不起作用.弹出窗口显示e.latlng.lat",而我想要确切的值.
你有什么解决办法吗?谢谢.

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.

推荐答案

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

});

相关文章