删除传单中的所有标记

时间:2019-11-26 13:04:08

标签: javascript leaflet

我有一张带有不同标记的地图,我需要删除它并显示其他地图。为此,我使用clearLayers()(我的脚本基于此question的答案)。

在下面添加我的代码。我要尝试做的是知道地图中是否有标记,如果答案是肯定的,请删除它并显示新标记。但是layerGroup始终是undefined ...如何删除它?我的layerGroup做得正确吗?

function markersMap(coords){
    // Delete previous markers
    if (layerGroup === undefined) {
        console.log('layerGroup undefined');
    } else{
        console.log('layerGroup defined');
        layerGroup.clearLayers();
    }        
    // markers
    var layerGroup = L.layerGroup().addTo(map); 
    var marker = "L.marker(coo,{icon: iconMarker}).bindPopup(popUp).addTo(layerGroup);";

    for (var i = 0; i < coords.length; i++) {
        popUp = String(coords[i][0]);
        coo = [coords[i][1],coords[i][2]];
        if (coords[i][3] <= 3) {
            var colorMarker = "red.png";
        } else if (coords[i][3] == 4) {
            var colorMarker = "orange.png";
        } else {
            var colorMarker = "green.png";
        }
        var iconMarker = new L.Icon({
            iconUrl: 'https://cdn.rawgit.com/pointhi/leaflet-color-markers/master/img/marker-icon-2x-'+colorMarker,
            iconSize: [25, 41],
            iconAnchor: [12, 41],
            popupAnchor: [1, -34],
            shadowSize: [41, 41]
        });
        eval("marker" + i + "=" + marker);        
    }    
}

谢谢!

0 个答案:

没有答案