Leaflet L.Icon标记未在地图重新加载中移除

时间:2017-07-03 11:42:53

标签: leaflet angular-leaflet-directive

地图包含两种类型的标记 圆圈图标添加:

      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]);
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

在删除之前,您正在检查_path属性。它会跳过L.Marker图层,因为它们没有_path属性,只有矢量图层(从L.Path扩展)。

如果您只需要从地图中删除某些图层类型,最好将它们分组到L.LayerGroupL.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);
        }
    });
}