AnsweredAssumed Answered

How to Create Duplicate Map in JSAPI

Question asked by ajmorris on Feb 12, 2015
Latest reply on Feb 12, 2015 by odoe

Using the JavaScript API, I have a map object, and I'd like to create a second map, which is a copy of the first map. Hopefully somebody knows of a simple way to do this.

 

After failing to discover a one-liner that copies the map, I thought I could spin through the layers, and add them to the second map. Like this:

var secondMap = new Map('secondMapNode');
for(var j = 0; j < firstMap.layerIds.length; j++) {
     var layer = firstMap.getLayer(firstMap.layerIds[j]);
     secondMap.addLayer(layer);
}
secondMap.setExtent(firstMap.extent);

This doesn't work, though. The secondMap acts strange, and the firstMap disappears. I guess that makes sense, because the layer gets confused about which map is belongs to. I'm hoping that I don't have the check the layer type, copy the tileInfo, and everything else.

 

An alternate approach that I wanted to try was to get the webmap json from the first map, and then create the second map from the json. That would work, but I can't find any code to get webmap json from a map (since the firstMap was created manually, not from webmap json).

Outcomes