[GIS] How To Use Tiled Map Services with Different Spatial References in ArcGIS API for JavaScript

arcgis-javascript-apicoordinate system

I have the following code fragment in an esri page to try and change basemaps, the following code works, unless the next basemap layer uses a different spatialreference.

if (baseMap)
  _map.removeLayer(baseMap);
baseMap = new esri.layers.ArcGISTiledMapServiceLayer(mapSource);
_map.addLayer(baseMap);

It seems like my only option is to destroy the map instance and start from scratch every time the user desires to change the basemap. Am I going about this the wrong way… anyone have any insight? I have seen samples of the basemapgallerywidget, but haven't used it.

So the basic question is: To change basemaps,not knowing the future basemap spatial reference, is my only option to destroy the map instance and re-create it?

Best Answer

Tiled map services added to a JS API map have to match the map's spatial reference. The map's spatial reference is set when you create the map if you specify an extent or it's set to the spatial reference of the first layer added to the map.

If you need to use tiled map services that are in different spatial references, then yes, you'll have to destroy and create a new map when you change tiled map services. Ideally, your tiled map services would all use the same spatial reference but it sounds like you might not have control over that.