Select to view content in your preferred language

Hiding, Removing, or Changing AGOL Basemap

1203
5
Jump to solution
05-28-2013 08:58 AM
DanielTrone
Deactivated User
Hi,

I'm using the Javascript API to load map layers from a Web Map.  I know this Web Map requires a default Basemap.  However, if I want to remove this basemap programmatically later, how would I do that? 

Right now I'm creating WebTiledLayer(s) and adding them with map.addLayer(basemap). 

Of course, this just adds another basemap on top of the default basemap defined in the WebMap.  (You can tell the default basemap is still there when panning around the map).
Is there a quick statement I can use to disable or remove the default basemap?

Thanks!
0 Kudos
1 Solution

Accepted Solutions
BradCollins
Emerging Contributor
Understood.

Have you tried removing the base map layer with Map#removeLayer()?

map.removeLayer( map.getLayer("layer0") );


It appears that "layer0" is always the base map's layer ID, but I don't know whether that is guaranteed. I took a look at the properties of each layer, and the best one I can find for identifying the base map layer is the url property. E.g., the url for the topographical map when loaded from the public JSAPI is this:

http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer

View solution in original post

0 Kudos
5 Replies
BradCollins
Emerging Contributor

I know this Web Map requires a default Basemap.


I don't think that's true. Apparently you just need to create a Map object without a "basemap" property in the constructor args.

I've created a Map without a base map with the following code:

var map = new esri.Map("map");


And then I add my layers after that.

Also, take a look at the sample here:

http://help.arcgis.com/en/webapi/javascript/arcgis/jssamples/layers_webtiled_many.html

It does not have a base map, and it loads a WebTiledLayer on demand when the user selects the web tile source. Here's the Map object construction code:

        map = new esri.Map("map", {   // note: no "basemap: 'streets'" in the following args
          slider: false,
          resizeDelay: 100, // 300ms(default) seemed a little slow in this case
          center: [-89.985, 29.579],
          zoom: 8
        });


The WebTiledLayer is added later:

        // alias for WebTiledLayer constructor
        var wtl = esri.layers.WebTiledLayer;

        ...

        var lyr = new wtl(l.url, l.options);


So then, try creating your Map without a "basemap" item in the constructor args, and I think you'll be fine.
0 Kudos
DanielTrone
Deactivated User
Thanks for the reply.

In the future, I plan to make all of my web-maps the way you describe.  However, right now I already have a 8,000+ line program with a lot of hardcoded references to layer ids loaded from an ArcGIS Online Web Map.  I might have to revisit and update a significant portion of my code in the future to make my program more flexible (it would be a good idea).  But right now I just want to release this app and this is the last problem left that still bothers me, so I am looking for a quick and easy solution for now.
0 Kudos
BradCollins
Emerging Contributor
Understood.

Have you tried removing the base map layer with Map#removeLayer()?

map.removeLayer( map.getLayer("layer0") );


It appears that "layer0" is always the base map's layer ID, but I don't know whether that is guaranteed. I took a look at the properties of each layer, and the best one I can find for identifying the base map layer is the url property. E.g., the url for the topographical map when loaded from the public JSAPI is this:

http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer
0 Kudos
DanielTrone
Deactivated User
That was just the code I needed, but with one adjustment; instead of 'layer0', it is 'defaultBasemap' for AGOL Web Maps.

Thanks for the help!
0 Kudos
BradCollins
Emerging Contributor
Very good. Glad to help!
0 Kudos