ui-leaflet icon indicating copy to clipboard operation
ui-leaflet copied to clipboard

How to remove all active layers or to know all active layers in leaflet?

Open nmccready opened this issue 8 years ago • 1 comments

From @princeshahnawaz2012 on February 17, 2015 10:18

I just need to know if there is any function which helps to return all active layers. basically, I need this function to remove all layers due to my ajax call. Here is my main function ...

function initialize(){
// set up the map var map = L.map('map').setView([51.505, -0.09], 13);

    L.tileLayer('https://{s}.tiles.mapbox.com/v3/{id}/{z}/{x}/{y}.png', {
        maxZoom: 18,
        attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
            '<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
            'Imagery © <a href="http://mapbox.com">Mapbox</a>',
        id: 'examples.map-i875mjb7'
    }).addTo(map);
        getMarkers();
        map.on('moveend', onMapMove); 

};

function getMarkers(){ var center = map.getCenter(); var zoo = map.getZoom(); var bounds = map.getBounds(); var min = bounds.getSouthWest().wrap(); var max = bounds.getNorthEast().wrap(); var Membersx = ""; var poly = ""; var values = []; var coord = []; var heatMap = ""; var HeatMapActiveLayer = $("#active_heatmap").val(); var heatMap = new L.TileLayer.HeatCanvas({},{ 'step': 0.1, 'degree': HeatCanvas.LINEAR, 'opacity': 0.5 });

$.ajax({
    type: "post", 
    url: "/geojsonData", 
    dataType: "json", 
    data: {clat:center.lat, clng:center.lng, zoom:zoo, minlat:min.lat, minlng:min.lng, maxlat:max.lat, maxlng:max.lng, layers:<?php echo json_encode($layers); ?>}, 
    success: function(result){

      //Removing existing Polygon Layers...
        $.each(result, function(index, layer) {
            for(i in map._layers) {
               if(typeof map._layers[i].feature != undefined){
                    if(typeof map._layers[i].feature.code == $("#active_heatmap").val()){
                        map.removeLayer(map._layers[i]);
                    }
                }   
            }
        });

        $.each(result, function(index, layer) {
            if(typeof(layer) != "undefined") {
                if(layer.data.entity == 'PYG'){ // Layer type is polygon
                    $.each(layer.data.features, function(index, polygons) {
                        var coordinates = JSON.parse(polygons.geometry.coordinates);
                        var values = [];
                        var coord = [];
                        for(var i = 0, l = coordinates.length; i < l; i++) {
                            values.push(coordinates[i]);
                        }   
                        coord.push(values);
                        var campus = {
                                'type': 'Feature',
                                'code': polygons.code,
                                'properties': {
                                        'name': polygons.properties.name, 
                                        'category': polygons.properties.category,
                                        'description': polygons.properties.description,
                                        'style': {
                                            weight: 2,
                                            color: polygons.properties.style.color,
                                            opacity: 1,
                                            fillColor: polygons.properties.style.color,
                                            fillOpacity: 0.4
                                        }},
                                'geometry': {
                                        'type': 'Polygon', 
                                        'coordinates': coord
                                    }
                        };

                        var poly =  L.geoJson(polygonData,  {
                                                    style: function (feature) {
                                                        return feature.properties && feature.properties.style;
                                                    },
                                                    onEachFeature: onEachPolygonFeature,
                        });
                        map.addLayer(poly); 
                    });
                }else{
                    if (layer.data.layer == $("#active_heatmap").val()) { // Layer type is activited for HeatMap
                        $.each(layer.data.features, function(features, latlng) {
                            heatMap.pushData(latlng.geometry.coordinates[1], latlng.geometry.coordinates[0], 3);
                            if(heatMap) { 
                                var mainLayers = {
                                    "heatMap": heatMap
                                };
                            } 
                        });
                        map.addLayer(heatMap);
                    }else{ 
                        Membersx = new L.geoJson(layer.data ,{
                            pointToLayer: function(feature, latlng) {
                                if(feature.geometry.type == 'Point'){ 
                                    return L.marker(latlng, {icon: L.ExtraMarkers.icon({
                                                                        icon: feature.marker_icon,
                                                                        markerColor: feature.marker_color,
                                                                        shape: feature.marker_type,
                                                                        prefix: 'fa'
                                                                    }) })
                                }
                            }, 
                            onEachFeature: onEachFeature
                        });
                        map.addLayer(Membersx);
                    }
                }
            }
        });
    }                       
});

}

Whenever i use the basic function to remove the active layer for the heatmap. I get this following error..

Uncaught TypeError: Cannot read property 'code' of undefined

if(typeof map._layers[i].feature != undefined){ if(typeof map._layers[i].feature.code == $("#active_heatmap").val()){ map.removeLayer(map._layers[i]); } }

Help me please to resolve this bug.

Copied from original issue: tombatossals/angular-leaflet-directive#636

nmccready avatar Oct 29 '15 16:10 nmccready

+1 for a way to get a list of active layers without looking at the DOM... It seems that baselayerchange and overlayadd events are not fired (or not received) at directive initialization time.

luxigo avatar Jun 12 '16 13:06 luxigo