Select to view content in your preferred language

Removing a Feature Layer?

5963
2
06-05-2012 11:28 AM
BrettGreenfield__DNR_
Frequent Contributor
Is there a way to do this?  I can remove layers added as dynamic or tiled map services, but I can't remove layers added as a Feature Layer (unless I use .removeAllLayers, which of course gets rid of everything, which I don't want).

Why doesn't .removeLayer work with Feature Layers?
0 Kudos
2 Replies
JenniferGaa
Occasional Contributor
Can you post some code that shows how you are trying to remove your feature layer?

I use a feature layer that is populated by a web service using a feature collection/layer definition.  I keep my featureLayer variable as a global and then am able to use removeLayer(featureLayer) to remove it from the map and re-add it as needed.
0 Kudos
BrettGreenfield__DNR_
Frequent Contributor
Thanks for the reply Jennifer - fortunately, I figured it out!

 var map;
 var oysterLayers;
 
    function init() {
   var extent = new esri.geometry.Extent({"xmin":409914.00497589994,"ymin":104607.44204058842,"xmax":482562.42004832183,"ymax":141217.9675552771,"spatialReference":{"wkid":26985}})
      map = new esri.Map("map",{extent:extent});
   
   var baseMap = new esri.layers.ArcGISTiledMapServiceLayer("http://www.mdimap.us/ArcGIS/rest/services/ImageryBaseMapsEarthCover/MD.State.MDiMap_Gazetteer83M/MapServer");
      map.addLayer(baseMap);
   oysterLayers = new esri.layers.FeatureLayer("http://www.mdimap.us/ArcGIS/rest/services/Biota/MD.State.BayOysters/MapServer/0"); 
   
 }
 
 function toggle(form) {
  if (form.checkThis.checked) {
   map.addLayer(oysterLayers)
  } else {
   map.removeLayer(oysterLayers)
  }
 }


The layer I was trying to toggle with a checkbox was the one defined by the oysterLayers variable.  I had originally defined that variable under the toggle function; for some reason when doing that, I could add the layer via the checkbox, but not remove it.  Any idea why that's the case?  I'm still quite new to JavaScript so I'm not able to see why that wouldn't work.
0 Kudos