3

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?

asked Jul 8, 2014 at 23:54
5
  • What have you tried so far? Where are you stuck? Switching Layers on & off is as simple as layer.setVisibility(value) so I'm not quite sure what kind of problem you are having Commented Jul 9, 2014 at 3:18
  • Layers added directly to the map object using the API can have their visibility toggled, but it does not appear to be the case for webmap layers added via the arcutils.createmap Commented Jul 9, 2014 at 3:50
  • Are you talking about a base map? or an layer added on top? Commented Jul 9, 2014 at 3:52
  • You need to provide more details before we can answer this question. Please see this post on tips to improve the question: meta.gis.stackexchange.com/questions/3349/… Commented Jul 9, 2014 at 3:55
  • I've added more details to my question, please reconsider. Commented Jul 9, 2014 at 15:21

3 Answers 3

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)
answered Jul 11, 2014 at 4:22
1

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);
answered Jul 10, 2014 at 18:33
1
  • 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 ArcGIS Commented Jun 28, 2015 at 16:44
1

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);
 }
 });
 });
answered Aug 1, 2014 at 21:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.