I've created a webmap using ArcGIS Online that I want to render in a web app using the ArcGIS for Javascript API. In ArcGIS Online, I've saved the webmap with all of the layers off (visibility: false). I want set the visibility of one layer ("Layer X" below) to true using the API.
arcgisUtils.createMap(mapid, "map").then(function(response){
layers = response.itemInfo.itemData.operationalLayers;
dojo.forEach(layers, function(layer){
if (layer.title == 'Layer X') {
layer.setVisibility(true)**;
}
console.log(layer.title + ", visibility: " + layer.visibility);
});
});
**I've tried using .show() as well.
Using the code snippet above, the visibility of "Layer X" doesn't change. The console log reports the layer title & visibility of all the layers except the one I'm trying to show.
The response object returned from the .createMap method is a Deferred object.
Is it possible to toggle the layers of a webmap using the ArcGIS for Javascript API, or is the response object immutable?
3 Answers 3
The layer object returned via the createMap response doesn't have the property "setVisibility."
However, it does has a property called "layerObject," which seems to be the actual map object. The code snippet below does the trick:
layer.layerObject.setVisibility(true)
In this situation I think the problem is that you are attempting to dig your layer out of the response itemInfo as opposed to the actual response map.
from this sample...
var myLayer = map.getLayer("USA_Tapestry_335");
myLayer.setVisibility(false);
-
That code is not in that sample, but OPs code is in this sample: developers.arcgis.com/javascript/jshelp/… . I'm also trying to wrap my head around what is the 'proper' way of doing things in ArcGISazium– azium2015年06月28日 16:44:58 +00:00Commented Jun 28, 2015 at 16:44
Another way to get the layers in the webmap is through arcgisUtils.getLegendLayers(). It generates a list of objects that include the title from ArcGIS Online, and the layer. Here's a snippet of what I would use accomplish what you're looking for.
arcgisUtils.createMap(mapid, "map").then(function(response){
var legendLayers = arcgisUtils.getLegendLayers(response);
dojo.forEach(legendLayers, function (item) {
if (item.title === "Layer X") {
item.layer.setVisibility(true);
}
});
});
Explore related questions
See similar questions with these tags.
layer.setVisibility(value)
so I'm not quite sure what kind of problem you are having