ArcGIS API 4.6 How to set opacity for the basemap?

2061
2
Jump to solution
06-05-2018 01:03 PM
BlairJones
New Contributor III

ArcGIS API 4.6  What is the "proper" way to change opacity for the basemap?

possibly:

var map = new Map({ basemap:"topo"});

map.allLayers.items[0].opacity=0.25;

or perhaps:

var map = new Map();

map.add( new TileLayer(

   id:"topo",

   url="https://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"

});

map.findLayerById("topo").opacity=0.25;

Appendix question: why use basemap vs TileLayer ?

Thanks and good health!

Blair

1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Blair,

   You can grab the basemap layer as you have in your first code block because you can always count on the the main basemap layer being 0 index but if you are using a basemap with reference layers you will have to adjust that logic. You can use map.baseLayers.forEach(layer) to set the opacity for each of the layers in the basemap. The reason you would use a basemap vs a TileLayer is mainly for the ease of coding using one line of code to set the basemap verses using a the TileLayer route.

View solution in original post

2 Replies
RobertScheitlin__GISP
MVP Emeritus

Blair,

   You can grab the basemap layer as you have in your first code block because you can always count on the the main basemap layer being 0 index but if you are using a basemap with reference layers you will have to adjust that logic. You can use map.baseLayers.forEach(layer) to set the opacity for each of the layers in the basemap. The reason you would use a basemap vs a TileLayer is mainly for the ease of coding using one line of code to set the basemap verses using a the TileLayer route.

arahman_mdmajid
Occasional Contributor

One more important thing to note is that you also have to wait until the view is ready and then access the basemap layer; otherwise, it will give you undefined.

view.when(function(){
    console.log(map.allLayers.length);
    console.log(map.basemap.baseLayers.getItemAt(0));
    map.basemap.baseLayers.getItemAt(0).opacity = 0.6;
});

 

Abdur Rahman
GIS Developer
0 Kudos