地图包含两种类型的标记 圆圈图标添加:
let circle = new customCircleMarker([item.latitude, item.longitude], {
color: '#2196F3',
fillColor: '#2196F3',
fillOpacity: 0.8,
radius: radM,
addressId: item.address_id
}).bindTooltip(`Address: <b>${item.address}</b><br/>
Patients from this address: <b>${item.total_patients}</b><br/>
Production: <b>$${item.total_production}</b><br/>`)
.addTo(this.mapInstance).on('click', this.circleClick);
图标标记添加在以下方法中:
//创建标记对象,将自定义图标作为选项传递,将内容和选项传递给弹出窗口,添加到地图
// create marker object, pass custom icon as option, pass content and options to popup, add to map
L.marker([item.latitude, item.longitude], { icon: chartIcon })
.bindTooltip(`Address: <b>${item.address}</b><br/>
Patients from this address: <b>${item.total_patients}</b><br/>
Production: <b>$${item.total_production}</b><br/>`)
.addTo(this.mapInstance).on('click', this.circleClick);
在清除地图上图标标记未被删除
地图清除功能:
if (this.mapInstance) {
for (let i in this.mapInstance._layers) {
if (this.mapInstance._layers[i]._path !== undefined) {
try {
this.mapInstance.removeLayer(this.mapInstance._layers[i]);
} catch (e) {
console.log('problem with ' + e + this.mapInstance._layers[i]);
}
}
}
}
答案 0 :(得分:0)
在删除之前,您正在检查_path
属性。它会跳过L.Marker
图层,因为它们没有_path
属性,只有矢量图层(从L.Path扩展)。
如果您只需要从地图中删除某些图层类型,最好将它们分组到L.LayerGroup
或L.FeatureGroup
等分组图层中,然后使用clearLayers
方法:
var layerGroup = new L.LayerGroup([
new L.Marker(...),
new L.CircleMarker()
]).addTo(map);
layerGroup.clearLayers();
如果这不是一个选项,你可以迭代地图的图层,然后检查图层的实例:
function customClearLayers () {
map.eachLayer(function (layer) {
if (layer instanceof L.Marker || layer instanceof L.CircleMarker) {
map.removeLayer(layer);
}
});
}