【问题标题】:Update properties in POPUP , with leaflet and geoJson使用传单和 geoJson 更新 POPUP 中的属性
【发布时间】:2020-11-01 10:50:12
【问题描述】:

我根据update properties of geojson to use it with leaflet制作了一个脚本

>>>Working script picture

但我有多个参数的问题。我想放置 2 个单独的变量,例如:

layer.feature.properties.desc = content.value;
layer.feature.properties.number = content2.value;

但是

layer.bindPopup(content).openPopup()

只能打开一个-“内容”,例如放的时候有错误:

layer.bindPopup(content + content2).openPopup();

>>> Picture

于是我又写了一个脚本:

function addPopup(layer)
{let popupContent = 
'<form>' + 
'Description:<br><input type="text" id="input_desc"><br>' +
'Name:<br><input type="text" id="input_cena"><br>' +
'</form>';

layer.bindPopup(popupContent).openPopup();
document.addEventListener("keyup", function() {

link = document.getElementById("input_desc").value;
cena = document.getElementById("input_cena").value;

layer.feature.properties.link = link;
layer.feature.properties.cena = cena;   
}); 
};

>>>Picture

但不幸的是:

layer.feature.properties.link = link;
layer.feature.properties.cena = cena; 

对于每个绘制的几何图形都是相同的。此外,当用户填写表格时,参数将在关闭 PopUp 后消失。使用update properties of geojson to use it with leaflet 脚本,每次用户“单击”PupUp 时,都可以看到写入的参数

谁能帮我解决这个问题?

【问题讨论】:

    标签: javascript leaflet popup geojson


    【解决方案1】:

    您必须在popupopen 事件中添加监听器。 将您的 addPopup 函数更改为:

    
    var openLayer;
    function addPopup(layer){
      let popupContent = 
      '<form>' + 
      'Description:<br><input type="text" id="input_desc"><br>' +
      'Name:<br><input type="text" id="input_cena"><br>' +
      '</form>';
      
      layer.bindPopup(popupContent).openPopup();
      
      layer.on("popupopen", function (e) {
        var _layer = e.popup._source;
        if(!_layer.feature){
            _layer.feature = {
            properties: {}
          };
        }
        document.getElementById("input_desc").value = _layer.feature.properties.link || "";
        document.getElementById("input_cena").value = _layer.feature.properties.cena || "";
        document.getElementById("input_desc").focus();
        openLayer = _layer;
      });
      
      layer.on("popupclose", function (e) {
        openLayer = undefined;
      })
      
    };
    
    L.DomEvent.on(document,"keyup",function(){
      if(openLayer){
        link = document.getElementById("input_desc").value;
        cena = document.getElementById("input_cena").value;
    
        openLayer.feature.properties.link = link;
        openLayer.feature.properties.cena = cena;   
      }
    })
    

    https://jsfiddle.net/falkedesign/ntvzx7cs/

    【讨论】:

    • 感谢您的回答,但上面的脚本仍然为每个几何图形提供相同的值。让我知道是否可以在下面编辑代码添加第二个文本区域:code function addPopup(layer) { var content = document.createElement(["textarea"]); var y = document.createElement(["textarea"]); content.addEventListener("keyup", function () { layer.feature.properties.link = content.value; }); layer.bindPopup(内容).openPopup(); }
    • @MateuszKruk 是的,你是对的,这是错误的。我更新了代码并修复了示例
    • 谢谢你,它有效!但是让我知道是否可以用相同的结果修改下面的代码?我的意思是 2 个独立属性的双文本区域? [链接]jsfiddle.net/mattiaccat/1ufa2cyw/1[链接]
    • 这里:Link 你必须将input 更改为textarea。我建议使用我的代码而不是您的小提琴中的代码,因为它使添加两个 DOM 元素 (createElement) 而不是添加 html 文本变得更加困难。请不要忘记接受这个答案
    • 感谢您的帮助和非常快速的答复!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-25
    • 2013-04-15
    • 1970-01-01
    • 2019-07-20
    • 1970-01-01
    • 2019-11-19
    • 1970-01-01
    相关资源
    最近更新 更多