Removing/Add Basemaps

1498
2
Jump to solution
02-14-2019 08:47 AM
by Anonymous User
Not applicable

Hi,

I'm looking to be pointed in the right direction. I'm working on a "Save Session" widget where I need to change the basemap in the webmap to whatever was saved previously.

In short, I need to first remove existing basemaps and then from my session info, update to new basemaps.

I'm not seeing (or just looking in the wrong place) the methods to work directly with basemaps in the webmap. I have found methods that allow me to get current basemaps, just nothing to add/remove them.

First, removal of basemaps using the following doesn't work, I receive "cannot read property id of undefined" (however the layers from getBasemapLayers() does indeed have IDs) and the same function works for operational layers. This leads me to believe I can't use map.removeLayer on basemap layers.

this.mapBaseMaps = layerInfosObj.getBasemapLayers();
arrayUtils.forEach(this.mapBaseMaps, function (l) {
    this.map.removeLayer(l.layerObject);
}, this);

Second, I'm struggling to get new basemap layers added in. I was  trying to add these layers using ArcGISTiledMapServiceLayer, but this doesn't seem to be the right approach. (the layers don't get added)

var baseMapLayer = new ArcGISTiledMapServiceLayer(basemaps[b].url);
 this.map.addLayer(baseMapLayer, this.mapBaseMaps.length-1);

Everything Basemap related I've found from WAB is using the Basemap Gallery widget. I haven't gone down this path, but I wonder if it could be used to handle my basemap removal / addition without actually presenting the widget to the user. I suggest this as an option, as like I said, I'm not seeing any methods that allow me to add / remove basemap layers. 

Or is there another approach I should be looking at?

thanks

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Kevin,

   The first issue is that the function you are calling getBasemapLayers is returning actual layer objects.

this.mapBaseMaps = layerInfosObj.getBasemapLayers();
arrayUtils.forEach(this.mapBaseMaps, function (l) {
    this.map.removeLayer(l);
}, this);‍‍‍‍

Next for adding a basemap you use:

var baseMapLayer = new ArcGISTiledMapServiceLayer("http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");
      baseMapLayer._basemapGalleryLayerType = "basemap";
      this.map.addLayer(baseMapLayer, 1);‍‍‍‍‍‍

 Yes you can absolutely use the BasemapGallery widget to add your basemap without showing the UI. Adam does that in the LocalLayer widget.

View solution in original post

2 Replies
RobertScheitlin__GISP
MVP Emeritus

Kevin,

   The first issue is that the function you are calling getBasemapLayers is returning actual layer objects.

this.mapBaseMaps = layerInfosObj.getBasemapLayers();
arrayUtils.forEach(this.mapBaseMaps, function (l) {
    this.map.removeLayer(l);
}, this);‍‍‍‍

Next for adding a basemap you use:

var baseMapLayer = new ArcGISTiledMapServiceLayer("http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");
      baseMapLayer._basemapGalleryLayerType = "basemap";
      this.map.addLayer(baseMapLayer, 1);‍‍‍‍‍‍

 Yes you can absolutely use the BasemapGallery widget to add your basemap without showing the UI. Adam does that in the LocalLayer widget.

by Anonymous User
Not applicable

Thanks for the super quick response, Robert.

Thanks for catching my layerObjects in the removeLayer. I should have noticed that.

And I dont think I would have caught on that you only need to set that type to 'basemap' to have addLayer properly insert the layer where I want.

0 Kudos