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....
Solved! Go to Solution.
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 = "";
}
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.
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, {"
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);
});
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 = "";
}
Fantastic,.....thanks a million cheers.