仅在传单弹出窗口中显示填充的属性(而不显示“空"属性) [英] Only show filled attributes in leaflet popups (and not "null"-attributes)
本文介绍了仅在传单弹出窗口中显示填充的属性(而不显示“空"属性)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的传单地图显示了来自GeoJSON文件的多边形.该文件具有几个属性(attribute_1,attribute_2等).但是,有些填充了文本,有些填充了空白.
My leaflet map shows polygons from a GeoJSON file. This file has several attributes (attribute_1, attribute_2, etc.). However, some are filled with text and some are empty.
如何只显示弹出窗口中用文本填充的属性,而不显示空文本?
在每个属性下面显示使用我的代码,如果为空,则在弹出窗口中显示"null":
Using my code beneath every attribute is shown and if it's empty "null" is shown in the popup:
// 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);
});
推荐答案
我使用一个简单的if ... else语句解决了这个问题.添加的内容以粗体突出显示:
I solved the question using a simple if...else statement. The additions are highlighted in bold:
// 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);
});
这篇关于仅在传单弹出窗口中显示填充的属性(而不显示“空"属性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文