首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Leaflet.draw中编辑弹出窗口

如何在Leaflet.draw中编辑弹出窗口
EN

Stack Overflow用户
提问于 2016-08-17 14:26:04
回答 1查看 2.6K关注 0票数 1

我正在使用传单和Leaflet.draw创建一个地图。当我在地图上(以用户身份)绘制一个矩形时,下面的代码将写出该矩形的LatLng边界。

代码语言:javascript
复制
    // add the newly drawn items to a layer
    map.on('draw:created', function (e) {
        var type = e.layerType,
            layer = e.layer;

        // binds things upon creation
        if (type === 'rectangle') {
            var bounds = layer.getBounds();
            layer.bindPopup(bounds.getNorthWest().toString() + "NW" + bounds.getSouthEast().toString() + "SE");
        }

        drawnItems.addLayer(layer);
    });

当矩形由用户编辑时,我想要更新它。我认为这样的东西应该使用‘抽签:编辑’事件和‘_popup.etContent’更新,但是LatLng中的内容不会更新。

代码语言:javascript
复制
    // update LatLng when edited
    map.on('draw:edited', function (e) {
        var type = e.layerType,
            layer = e.layer;

        // update popup
        if (type === 'rectangle') {
            var bounds = layer.getBounds();
            layer._popup.SetContent(bounds.getNorthWest().toString() + "NW" + bounds.getSouthEast().toString() + "SE");
        }
    });

添加第二个代码块也意味着我只能编辑创建的第一个矩形。所以这显然不起作用,但我不知道为什么。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-17 14:50:42

我想通了。对于抽签:编辑事件,我需要使用eachLayer方法来迭代e.layers中的每个层,然后使用instanceof来检查层类型。我还绑定了一个新的弹出窗口,而不是编辑旧的弹出窗口。

下面是代码,它在弹出窗口中也有一个断行符,以提高其可读性:

代码语言:javascript
复制
    // update LatLng value
    map.on('draw:edited', function (e) {
        var layers = e.layers;

        layers.eachLayer(function (layer) {
            // do whatever you want to each layer, here update LatLng
            if (layer instanceof L.Rectangle) {
                var bounds = layer.getBounds();
                layer.bindPopup(bounds.getNorthWest().toString() +  " NW<br>" + bounds.getSouthEast().toString() + " SE");
            }
        });
    });

感谢有关在编辑时检索层类型的问题为我指明了正确的方向。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38999409

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档