AnsweredAssumed Answered

Switching Basemaps Programmatically

Question asked by havel.chris on Jun 6, 2014
Latest reply on Dec 16, 2014 by ykchin
I am trying to change basemaps without the BaseMap widget.

I can accomplish this BUT I have two buttons and I only want ONE..

I am trying to invision how to do this with one button....
1. I should be able to use a toggler of some sort
2. if not I can use an If Then statement or a Case as seen below BUT would need to fidn out how to determine the name of the current basemap that is being displayed..

    
    map = new esri.Map("map", {
       basemap: "topo",
    });


If I am looking at the code above....

1. How do I test for the basemap? Return to a variable and then I can use in the Case below.

I tried this and got 'undefined'


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);



WORKING CODE BUT TWO BUTTONS

 <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);

      }

Outcomes