我的传单地图显示了来自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);
});
创建验证:
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);
}
我用一个简单的if解决了这个问题。否则声明。新增内容以粗体突出显示:
// 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);
});