2017-07-03 45 views
0

地图包含两种类型的标记 圆圈图标被添加:传单L.Icon标记不删除地图加载

 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

你检查一个_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); 
     } 
    }); 
} 
+0

感谢您的信息。得到它固定(y)@ iH8 –