Leaflet - 在弹出窗口中获取标记的纬度和经度 [英] Leaflet - get latitude and longitude of a marker inside a pop-up

查看:13
本文介绍了Leaflet - 在弹出窗口中获取标记的纬度和经度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

});

这篇关于Leaflet - 在弹出窗口中获取标记的纬度和经度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆