2017-07-03 89 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 –