Clearing Feature Layer

3240
5
Jump to solution
12-05-2014 11:28 AM
jaykapalczynski
Frequent Contributor

I am populating a Feature Layer with the spatial features contained withing a Shapefile.

Attached is a snip of that code:  In that code I am assigning the shapefile to the Feature Layer "featureLayerSF"

Everything works fine and I can add the shapefiles.  Although in my case I only want to have one shapefile added at a time. 

My question is how do I remove the data inside "featureLayerSF" before the newly chosen shapefile spatial contents are added to the "featureLayerSF"

Any ideas would be very appreciated.  Or code snips....

0 Kudos
1 Solution

Accepted Solutions
HeikoHeijenga
Esri Contributor

Store the layers you create in a (global) array:

...

allShapeFileLayers = [];

...

...

function addShapefileToMap(featureCollection) {

    var fullExtent;

    var layers = [];

    arrayUtils.forEach(allShapeFileLayers, function(layer) {

      map.removeLayer(layer);

    });

    arrayUtils.forEach(featureCollection.layers, function(layer) {

        var infoTemplateSF = new InfoTemplate("Details", "${*}");

        var featureLayerSF = new FeatureLayer(layer, {

            infoTemplate: infoTemplateSF

        });

        //associate the feature with the popup on click to enable highlight and zoom to

        featureLayerSF.on('click', function(event) {

            app.map.infoWindow.setFeatures([event.graphic]);

        });

        //change default symbol if desired. Comment this out and the layer will draw with the default symbology

        changeRenderer(featureLayerSF);

        fullExtent = fullExtent ?

            fullExtent.union(featureLayerSF.fullExtent) : featureLayerSF.fullExtent;

        layers.push(featureLayerSF);

    });

   allShapeFileLayers = layers;

    app.map.addLayers(layers);

    app.map.setExtent(fullExtent.expand(1.25), true);

    dom.byId('upload-status').innerHTML = "";

}

View solution in original post

5 Replies
HeikoHeijenga
Esri Contributor

Looking at your code snippet it seems like every time you add a Shapefile you create and add a new layer to the map. If you only want to show the last added Shapefile you should remove the previously added layer.

0 Kudos
jaykapalczynski
Frequent Contributor

I tried this :  app.map.removeAllLayers()

But it removed all my layers including my base map etc....I dont know the index of the layer I am trying to remove....

OR CAN i look for the name of the layer (featureLayerSF) :  "var featureLayerSF = new FeatureLayer(layer, {"

0 Kudos
jaykapalczynski
Frequent Contributor

I tried the below:  trying to removeLayer but this does not work it keeps adding to the layer

arrayUtils.forEach(featureCollection.layers, function (layer) {

  var infoTemplateSF = new InfoTemplate("Details", "${*}");

  var featureLayerSF = new FeatureLayer(layer, {

  infoTemplate: infoTemplateSF

  });

  //associate the feature with the popup on click to enable highlight and zoom to

  featureLayerSF.on('click', function (event) {

  app.map.infoWindow.setFeatures([event.graphic]);

  });

  //change default symbol if desired. Comment this out and the layer will draw with the default symbology

  changeRenderer(featureLayerSF);

  fullExtent = fullExtent ?

  fullExtent.union(featureLayerSF.fullExtent) : featureLayerSF.fullExtent;

  app.map.removeLayer(featureLayerSF)

  layers.push(featureLayerSF);

});

0 Kudos
HeikoHeijenga
Esri Contributor

Store the layers you create in a (global) array:

...

allShapeFileLayers = [];

...

...

function addShapefileToMap(featureCollection) {

    var fullExtent;

    var layers = [];

    arrayUtils.forEach(allShapeFileLayers, function(layer) {

      map.removeLayer(layer);

    });

    arrayUtils.forEach(featureCollection.layers, function(layer) {

        var infoTemplateSF = new InfoTemplate("Details", "${*}");

        var featureLayerSF = new FeatureLayer(layer, {

            infoTemplate: infoTemplateSF

        });

        //associate the feature with the popup on click to enable highlight and zoom to

        featureLayerSF.on('click', function(event) {

            app.map.infoWindow.setFeatures([event.graphic]);

        });

        //change default symbol if desired. Comment this out and the layer will draw with the default symbology

        changeRenderer(featureLayerSF);

        fullExtent = fullExtent ?

            fullExtent.union(featureLayerSF.fullExtent) : featureLayerSF.fullExtent;

        layers.push(featureLayerSF);

    });

   allShapeFileLayers = layers;

    app.map.addLayers(layers);

    app.map.setExtent(fullExtent.expand(1.25), true);

    dom.byId('upload-status').innerHTML = "";

}

jaykapalczynski
Frequent Contributor

Fantastic,.....thanks a million cheers.

0 Kudos