在传单地图中弹出 Leaflet.markercluster
Popup with Leaflet.markercluster in leaflet map
我是 leaflet 的初学者,我测试了这段代码 ,以创建一个集群生成器。但是,当我单击标记时,弹出窗口 window 中的信息始终相同。您能否告诉我要更改代码中的哪些内容才能在每次点击时显示不同的弹出窗口 window?请帮我。
代码在这里:
var OpenStreetMap_Mapnik = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
var street = {...my_geojson_data...}
var s_light_style = {
radius: 8,
fillColor: "#ff7800",
color: "#000",
weight: 1,
opacity: 1,
fillOpacity: 0.8
};
var markers = L.markerClusterGroup();
L.geoJSON(street, {
onEachFeature : function(feature, layer){
var popupContent = '<h4 class = "text-primary">Street Light</h4>' +
'<div class="container"><table class="table table-striped">' +
'<thead><tr><th>Properties</th><th>Value</th></tr></thead>' +
'<tbody><tr><td> Name </td><td>'+ feature.properties.Name +'</td></tr>' +
'<tr><td>Elevation </td><td>' + feature.properties.ele +'</td></tr>' +
'<tr><td> Power (watt) </td><td>' + feature.properties.Power_Watt + '</td></tr>' +
'<tr><td> Pole Height </td><td>' + feature.properties.pole_hgt + '</td></tr>' +
'<tr><td> Time </td><td>' + feature.properties.time + '</td></tr>';
layer.bindPopup(popupContent)
},
pointToLayer: function (feature, latlng) {
return markers.addLayer(L.circleMarker(latlng, s_light_style))
}
})
map.addLayer(markers);
您的 pointToLayers
函数 return 是您的 markers
MarkerClusterGroup,而您应该 return 只有单个(圆形)标记。因此,稍后您的 onEachFeature
函数会将弹出窗口绑定到整个组。
一个简单的解决方案是避免在 构建 GeoJSON 层组时 填充 MCG,而是仅在 末尾 添加它:
const geojsonGroup = L.geoJSON({
pointToLayer: function (feature, latlng) {
return L.circleMarker(latlng);
}
});
markers.addLayer(geojsonGroup);
map.addLayer(markers);
我是 leaflet 的初学者,我测试了这段代码
var OpenStreetMap_Mapnik = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
var street = {...my_geojson_data...}
var s_light_style = {
radius: 8,
fillColor: "#ff7800",
color: "#000",
weight: 1,
opacity: 1,
fillOpacity: 0.8
};
var markers = L.markerClusterGroup();
L.geoJSON(street, {
onEachFeature : function(feature, layer){
var popupContent = '<h4 class = "text-primary">Street Light</h4>' +
'<div class="container"><table class="table table-striped">' +
'<thead><tr><th>Properties</th><th>Value</th></tr></thead>' +
'<tbody><tr><td> Name </td><td>'+ feature.properties.Name +'</td></tr>' +
'<tr><td>Elevation </td><td>' + feature.properties.ele +'</td></tr>' +
'<tr><td> Power (watt) </td><td>' + feature.properties.Power_Watt + '</td></tr>' +
'<tr><td> Pole Height </td><td>' + feature.properties.pole_hgt + '</td></tr>' +
'<tr><td> Time </td><td>' + feature.properties.time + '</td></tr>';
layer.bindPopup(popupContent)
},
pointToLayer: function (feature, latlng) {
return markers.addLayer(L.circleMarker(latlng, s_light_style))
}
})
map.addLayer(markers);
您的 pointToLayers
函数 return 是您的 markers
MarkerClusterGroup,而您应该 return 只有单个(圆形)标记。因此,稍后您的 onEachFeature
函数会将弹出窗口绑定到整个组。
一个简单的解决方案是避免在 构建 GeoJSON 层组时 填充 MCG,而是仅在 末尾 添加它:
const geojsonGroup = L.geoJSON({
pointToLayer: function (feature, latlng) {
return L.circleMarker(latlng);
}
});
markers.addLayer(geojsonGroup);
map.addLayer(markers);