Reference layer doesn't get removed when changing basemap

410
5
Jump to solution
05-02-2018 07:33 AM
MollyFoley
Regular Contributor

I instantiate my map with the basemap set to "hybrid". The hybrid basemap automatically has a reference layer added to display labels for various places. Now, if I switch the basemap via a basemap gallery to something like "imagery" where there should be no reference labels, the reference labels still persist. If I switch it to other basemaps, again, the references persist whether they're supposed to or not. How can I remove the reference layer for basemaps that aren't supposed to have it?

If I instantiate my map with "satellite" first, all the basemaps work as expected when switching between them (things that are supposed to have labels, have labels. Things that aren't supposed to have labels don't). 

I see there is a property that would allow me to remove the reference layer when switching maps, but since the reference layer is automatically added, I don't know what the 'id' of it is.

Code (the reference layer persists when switching basemaps):

//Instantiate the map
map = new Map("map", {
    basemap: "hybrid",
    center: [-85.603281, 36.241294],
    zoom: 6
});

//Create basemap gallery
var basemapGallery = new BasemapGallery({
    showArcGISBasemaps: true,
    map: map
}, "gallery");
basemapGallery.startup();‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Code (the basemaps work as expected, reference layer does not persist if it's not supposed to):

//Instantiate the map
map = new Map("map", {
    basemap: "satellite",
    center: [-85.603281, 36.241294],
    zoom: 6
});

//Create basemap gallery
var basemapGallery = new BasemapGallery({
    showArcGISBasemaps: true,
    map: map
}, "gallery");
basemapGallery.startup();‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Honored Contributor

This has been identified as a bug (BUG-000089550) almost two years ago. It's still listed as "In Product Plan". This discussion gives a workaround.

View solution in original post

5 Replies
KenBuja
MVP Honored Contributor

This has been identified as a bug (BUG-000089550) almost two years ago. It's still listed as "In Product Plan". This discussion gives a workaround.

View solution in original post

MollyFoley
Regular Contributor

I'm still having some trouble with this. I have the reference layer, and I can delete it successfully on the first basemap selection-change event. Then if I change it again, the reference layer never comes back, or if I change it to a basemap like "Terrain with Labels" it's own reference layer starts persisting, rather than being deleted upon change like my code dictates. This is what I have:

basemapGallery.on('selection-change', function () {
    var bmapTitle = basemapGallery.getSelected().title;
    var referenceLayer = map.getLayer("layer1");
    if (bmapTitle != "Imagery with Labels") {
        map.removeLayer(referenceLayer);
        console.log(map.basemapLayerIds);
    }
});‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I read in the discussion you linked to that maps.basemapLayerIds property doesn't change which seems to be reflected in my testing as if I change the basemap from "hybrid" to "satellite" on the first change, "layer 0", "layer1" are still contained in the array returned by map.basemapLayerIds when really only "layer0" should be there since the reference layer ("layer1") was removed. So if this is the case, how do you know if "layer1" truly exists or not to remove in future changes?

On the third change of a basemap I get this:

TypeError: Cannot read property 'id' of undefined

0 Kudos
KenBuja
MVP Honored Contributor

The fix doesn't use the BasemapGallery 'selection-change' event. Rather, it uses the map 'load' event. Take a look at the BasemapGallery sample. In the map construction, replace "topo" with "hybrid" to see the bug (line 34). Now insert the following code:

map.on('load', function (){
  var basemaps = map.basemapLayerIds;
  setTimeout(function () {
    for (var index = 0; index < basemaps.length; index++) {
      var layer = map.getLayer(basemaps[index]);
      layer._basemapGalleryLayerType = "basemap";
    }
  }, 2000);      
});

The reference layer is removed correctly when changing basemaps and there are no errors in the console after multiple changes.

MollyFoley
Regular Contributor

Thanks for the help. I misunderstood that post as I thought you were employing that workaround because you were loading basemaps from a configuration file, not because that's the way to do it! Although I did manage to figure out a different workaround too (not nearly as elegant as yours):

DELETED

Edit: this solution is much much simpler (thanks to Ken's advice below):

on.once(basemapGallery, 'selection-change', function () {
    var referenceLayer = map.getLayer(map.layerIds[1]);
    map.removeLayer(referenceLayer);
});‍‍‍‍
0 Kudos
KenBuja
MVP Honored Contributor

The bug occurs when the map is constructed using the hybrid, gray, dark-gray, oceans, and terrain maps. You don't cover the gray ones in your code. Also, since the bug doesn't happen after the first change, you're running the code unnecessarily after each change.

You could use the on.once method to run it just on the initial change.