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?

  • 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 – Devdatta Tengshe Jul 9 '14 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 – melo Jul 9 '14 at 3:50
  • Are you talking about a base map? or an layer added on top? – Devdatta Tengshe Jul 9 '14 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/… – Devdatta Tengshe Jul 9 '14 at 3:55
  • I've added more details to my question, please reconsider. – melo Jul 9 '14 at 15:21
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)
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);
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);
         }
     });

 });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.