I want to use javascript and dojo to add and remove layers.

  • I want how to add layers dynamically in arcGis API VERSION 4.0.please send me an example as soon as possible...please request – user83058 Sep 22 '16 at 12:53
  • Please do not leave questions as answers. Also, asking people to do things ASAP does generally not help, as everyone here is helping out in their spare time. Please read the tour. – John Powell Sep 22 '16 at 13:00

Use esri.Map's addLayer and removeLayer methods. Both take the layer to add or remove from the map as an argument. You might also be interested in map.removeAllLayers() and map.reorderLayer.

Finally, one thing that sometimes trips people up is getting a reference to layers in the map. The recommended approach is to assign an ID to your layers when you create them. If you don't manually assign an ID, the map assigns one for you. You can get these id's by looking at map.layerIds and map.graphicsLayerIds. Both are arrays of strings. Once you have an ID, get the associated layer using map.getLayer("some_layer_id").

  • 1
    it might also be constructive to say that layers in the js api are map services (a group of arc map data layers) not the individual data layers within the map service. Currently you can't modify what layers are in that map service but I think that is changing at 10.1 – Steve Feb 23 '12 at 20:42
  • Derek, Upon your suggestion, I tried the following ways to remove a layer (mapservice) from the map: 1. map.removeAllLayers(); 2. for (var j = 0; j < map.layerIds.length; j++) { var layer = map.getLayer(map.layerIds[j]); map.removeLayer(layer); } I had NO success removing the mapservices from the Map. After executing the code above, I could still see the mapservices. Any ideas? – user6755 Apr 4 '12 at 21:35
  • 1
    @LearningGIS24 can you post this as a separate question? Comments on an answer aren't really the place for new discussion. – Derek Swingley Apr 6 '12 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.