我有这个脚本按照我的意愿工作,但是我想知道如何将Leaflet图层合并为一个以使其成为更好的脚本,而不使用组合器:
var namelayer= L.geoJson(parcels, {
onEachFeature: function (feature, layer) {
layer.setText(feature.properties.NAME, {attributes: {'fontsize': '15', 'fill': 'white'}, offset: 5}) },
style: {
weight: 1,
color: 'blue' ,
attributes: {'font-size': '24'}
}
}).addTo(map);
function POPUP(feature, layer) {
var popupContent = "<h3>Info</h3>"
+ '</b><br />' + 'area: ' + feature.properties.m2 + ' m2';
layer.bindPopup(popupContent);
};
var popuplayer = L.geoJson(parcels, {
onEachFeature: POPUP,
style: {
weight: 1,
color: 'blue' ,
attributes: {'font-size': '24'}
}
}).addTo(map);
使用属性功能的数据从json文件中检索变量parcel。
setText部分来自:this Leaflet plugin
我试图通过将这行代码复制到POPUP函数中来将变量名称层集成到popuplayer中
layer.setText(feature.properties.NAME, {attributes: {'fontsize': '15', 'fill': 'white'}, offset: 5}) }
这会产生此代码,但无效:
function POPUP(feature, layer) {
var popupContent = "<h3>Info</h3>" + '</b><br />' + 'area: ' + feature.properties.m2 + ' m2';
layer.setText(feature.properties.NAME, {attributes: {'font-size': '15', 'fill': 'white'}, offset: 5}) };
layer.bindPopup(popupContent);
};
var popuplayer = L.geoJson(parcels, { onEachFeature: POPUP,
style: {
weight: 1,
color: 'blue' ,
attributes: {'font-size': '24'}
}
}).addTo(map)
有人知道我做错了什么或遗忘了吗?