map = new esri.Map("map", { basemap: "topo", });
var layer = map.getLayer('basemap'); var layerUrl; switch(basemap){ case 'topo': layerUrl = 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer'; break; case 'imagery': layerUrl = 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer'; break; } var basemap = new esri.layers.ArcGISTiledMapServiceLayer(layerUrl,{id:'basemap'}); map.addLayer(basemap,0); alert(layer);
<div dojotype='dijit.layout.ContentPane' region='top' style='height:20px;'> <input type='button' value='Topo' onclick='switchMap("topo");'/> <input type='button' value ='Imagery' onclick='switchMap("imagery");'/> </div>
map = new esri.Map("map",{ extent:initExtent }); var basemap = new esri.layers.ArcGISTiledMapServiceLayer("https://server.arcgisonline.com/ArcGIS/rest/services/World_Terrain_Base/MapServer",{id:'basemap'}); map.addLayer(basemap); function switchMap(basemap){ //remove the existing basemap layer var layer = map.getLayer('basemap'); map.removeLayer(layer); var layerUrl; switch(basemap){ case 'topo': layerUrl = 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer'; break; case 'imagery': layerUrl = 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer'; break; } var basemap = new esri.layers.ArcGISTiledMapServiceLayer(layerUrl,{id:'basemap'}); map.addLayer(basemap,0); }
Solved! Go to Solution.
Thank you this has helped to find the ids names! Great!
//But when I'm using the default-widget.
var basemapGallery = new BasemapGallery({
showArcGISBasemaps: true,
map: map
}, "basemapGallery");
basemapGallery.startup();
//I cannot add
basemapGallery.remove("basemap_0");
//to remove a basemap. It return Null!
//I think basemap_0 is not yet recognized after the startup()
//Do you have a solution for that?
//Thank you
I would suggest starting a new discussion on this. It would help get the attention of the Javascript API experts at Esri.
new discussion; Adapt basemap collection in Dijit BasemapGallery
thanks.
Take a look at my demo site. I just the basemap toggle and then just applied CSS to override the defaults.
Javascript
var toggle = new BasemapToggle({
map: app.map,
basemap: "hybrid"
}, "BasemapToggle");
toggle.startup();
CSS
#BasemapToggle {
position: absolute;
top: 20px;
right: 20px;
z-index: 50;
}
.BasemapToggle .basemapImage {
height: 0;
overflow: hidden;
width: 0px;
}
.BasemapToggle .basemapTitle {
width: 80px;
}
Just want to add to Andrew Pratt's code.
You could add an event listener to the toggle and do switch there between 2 or more base layer.
Example:
toggler.on("toggle", changeNextBasemap);
function changeNextBasemap(event){
switch(event.currentBasemap){
case 1:
toggle.basemap = "whatever";
break;
case 2:
case 3:
case 4:
...
}
}