setBasemap() not working on webmap with string or object value

2463
13
07-11-2013 08:06 AM
deleted-user-RAnWn8DDSd1P
New Contributor III
http://www.portlandmaps.com/arcmaps/setBasemap.html

You can see in console both map objects reflect the change of basemap in _basemap and _layers has the new layer, but neither the string value 'topo' or an object of type esri/utils/Basemap work as a param to setBasemap() to successfully swap it out. what am i missing here?
0 Kudos
13 Replies
deleted-user-RAnWn8DDSd1P
New Contributor III
nothing is wrong with the webmap if arcgis online let me build it this way. 

moved to jsfiddle http://jsfiddle.net/akkakk/pnnr6/
0 Kudos
HaroldBostic
Occasional Contributor II
The issue is webMaps by default don't honor the named values for basemaps.  You have to set a base map using setBaseMap before you can set another base maps.  My workaround is to use a dictionary that maps base maps titles to their valid string equivalent
https://developers.arcgis.com/en/javascript/jsapi/map-amd.html#setbasemap

then I remove the base map and useSetBaseMap of the equivalent


   //get title of basemap
var currentBasemapName = webmapResponse.itemInfo.itemData.baseMap.title
var bmNameEqiv = bmDict[currentBasemapName ];             esriMap.removeLayer(webmapResponse.itemInfo.itemData.baseMap.baseMapLayers[0].layerObject);
                //just reset the base map so we can use setBaseMap later
                esriMap.setBasemap(bmNameEqiv);
0 Kudos
YuqianHuang
New Contributor

Thanks for the answer, which is still helpful three more years later. This solution fixed the problem that setBaseMap cannot recognize the basemap from web map. 

I think the possible mechanism of setBaseMap() might be: Delete the basemap of map.basemapLayerIds, and then import the new one. 

0 Kudos
StevenFabijanski1
New Contributor

I had a similar problem setting the basemap using map.setBasemap and stumbled on this thread. After a bit of investigation I discovered that when using a webmap the map object holds a reference to the initial basemap as 'defaultBasemap'. You can see that by examining map.layerIds.  As long as that layer remains in the map, setting a new basemap with `map.setBasemap('topo')` will not work properly. The new layer gets added, but the 'defaultBasemap' layer remains. The easiest solution is removing the 'defaultBasemap' layer just before setting a new basemap. Here's how I achieved it:

const baseMap = map.getLayer('defaultBasemap');
if( baseMap ) {
map.removeLayer(baseMap);
}
map.setBasemap('streets');
0 Kudos