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
    Commented Sep 22, 2016 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. Commented Sep 22, 2016 at 13:00

2 Answers 2


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
    Commented Feb 23, 2012 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
    Commented Apr 4, 2012 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. Commented Apr 6, 2012 at 16:07

In Version 4.x, use the following map methods:


For more info, check out the 4.x JS API Methods for Map.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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