【问题标题】:Only show filled attributes in leaflet popups (and not "null"-attributes)仅在传单弹出窗口中显示填充属性(而不是“空”属性)
【发布时间】:2016-06-01 08:02:37
【问题描述】:

我的传单地图显示来自 GeoJSON 文件的多边形。该文件有几个属性(attribute_1、attribute_2 等)。但是,有些是用文字填充的,有些是空的。

如何在弹出窗口中只显示用文本填充的属性而不显示空属性?

在每个属性下使用我的代码都会显示,如果它是空的,弹出窗口中会显示“null”:

// Integrate GeoJSON and style polygons
  $.getJSON("klimagutachten_2001.geojson",function(klimagutachten){
    L.geoJson( klimagutachten, {
        style: function(feature){
            return {
                color: "#e60000",
                weight: 4,
                fillColor: "#e60000",
                fillOpacity: .3
            };
        },

// Call popup
        onEachFeature: function( feature, layer ){
            layer.bindPopup("<strong> Attribute 1: \"" + feature.properties.attribute_1 + " and the second attribute is: " + feature.properties.attribute_2)
        }                                                                                                       
    }).addTo(map);
  });

【问题讨论】:

  • 已经发布了一个类似的问题 (stackoverflow.com/questions/34055382/…),但它没有回答我的问题。
  • 你的意思是你试过了还是不行?
  • 它应该可以工作,但我不希望属性不按要求显示。我想要弹出窗口中每个不为空的属性。所以这是一个稍微不同的问题。

标签: null popup leaflet geojson


【解决方案1】:

创建验证:

onEachFeature: function( feature, layer ){

var text = "";

if (!feature.properties.attribute_1) {
    text += feature.properties.attribute_1 +" "; 
}
if (!feature.properties.attribute_2) {
    text += feature.properties.attribute_2 +" "; 
}
layer.bindPopup(text);

} 

【讨论】:

  • 由于我有限的编程知识,很遗憾我无法运行代码。您能否协助将其准确粘贴到哪里。谢谢!
  • 感谢编辑,但它不起作用。具有这两个属性的多边形现在完全为空,只有这两个属性之一的多边形仅显示“空”...
【解决方案2】:

我用一个简单的 if...else 语句解决了这个问题。添加的内容以粗体突出显示:

// Integrate GeoJSON and style polygons
  $.getJSON("klimagutachten_2001.geojson",function(klimagutachten){
    L.geoJson( klimagutachten, {
        style: function(feature){
            return {
                color: "#e60000",
                weight: 4,
                fillColor: "#e60000",
                fillOpacity: .3
            };
        },

// Call popup
        onEachFeature: function( feature, layer ){
        var attribute_1 = feature.properties.attribute_1;
        if (attribute_1 == null) {
            attribute_1 = "";
        } else {
            var attribute_1 = feature.properties.attribute_1;
        };

            layer.bindPopup("<strong> Attribute 1: \"" + attribute_1 + " and the second attribute is: " + feature.properties.attribute_2)
        }   
    }).addTo(map);
  });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    • 2010-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多