Turn On Layers on Widget Start

1273
13
Jump to solution
10-04-2016 11:27 AM
Highlighted
New Contributor III

How can we turn on layers from a different widget (other than the layer list) say - for example my widget only works when this certain layer is on (by default they are all off)

Thank you

Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
MVP Esteemed Contributor

Gabi,

    Historical Aerials will NOT be the actual id of the layer. It would look like Parcel Data_0.

You can retrieved the layer id from web map id as shown below. Where 32a83775654249dcae6b8f2eff5d4072 is your web map id.

http://<your portal url>/sharing/rest/content/items/32a83775654249dcae6b8f2eff5d4072/data/?f=pjson

View solution in original post

Reply
0 Kudos
13 Replies
Highlighted
MVP Esteemed Contributor

Gabi,

   Sure see this thread:

Make layer visible after LayerList widget has opened 

and this thread to see how to get layerInfosObject from some other widget:

https://community.esri.com/thread/183044-how-to-remove-all-layers-from-a-web-appbuilder-app-dev

So together the code would look something like this:

       require(["jimu/LayerInfos/LayerInfos"], lang.hitch(this, function(LayerInfos) {
          LayerInfos.getInstance(this.map, this.map.itemInfo).then(function(layerInfosObject){
             layerInfosObject.getLayerInfoById('Parcel Data_0').setTopLayerVisible(true);
          });
        }));
Reply
0 Kudos
Highlighted
New Contributor III

Robert !

Where should the code with ......

......require (["jimu/LayerInfos/LayerInfos"], ..............

go, what file in the widget.js of the LayerList widget?

Thank you so very, very much for your answer

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

Gabi,

   I thought you were asking for a solution that did not require it to be in the LayerList widget. The code I provided can be used inside and widgets Widget.js file.

Reply
0 Kudos
Highlighted
New Contributor III

I"m so sorry Robert - maybe I did not explain very well...my question.

I would like to automatically turn on the "Historial Aerials" layer in my LayerList widget whenever I click on the TimeSlider. Right now I get the "No time-aware layers are visible" and we have to manually turn it on before using the TimeSlider and I  would like to automatically make the time aware layer visible when the TimeSlider widget is activated ...............only if not too difficult to do it....or understand !

So my question is - where should I add the code that you have provided - witch of the two widgets (LayerList or TimeSlider) widget.js file?

 I'm so sorry for the confusion and thank you again ....

You're really the best !

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

Gabi,

   For the TimeSlider widget you would add these lines to the onOpen function of the Widget.js (for the TimeSlider widget) (lines 3 - 5):

      onOpen: function() {
        this._initLayerInfosObj().then(lang.hitch(this, function() {
          if(!this.layerInfosObj.getLayerInfoById('Parcel Data_0').isVisible()){
            this.layerInfosObj.getLayerInfoById('Parcel Data_0').setTopLayerVisible(true);
          }
          if (!this.hasVisibleTemporalLayer()) {
            html.setStyle(this.noTimeContentNode, 'display', 'block');
            html.setStyle(this.timeContentNode, 'display', 'none');
            this._showed = true;
          } else {
            if (!this._showed) {
              this.showTimeSlider();
            }
          }
        }));
      },‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Highlighted
New Contributor III

Thank you Robert !

I just tried that but it does not seem to work!

...................................................

onOpen: function() {

this._initLayerInfosObj().then(lang.hitch(this, function () {

//if (!this.layerInfosObj.getLayerInfoById('Historical Aerials').isVisible()) {

// this.layerInfosObj.getLayerInfoById('Historical Aerials').setTopLayerVisible(true);

//}

if (!this.hasVisibleTemporalLayer()) {

html.setStyle(this.noTimeContentNode, 'display', 'block');

html.setStyle(this.timeContentNode, 'display', 'none');

this._showed = true;

} else {

if (!this._showed) {

this.showTimeSlider();

}

}

}));

},

........................................

Maybe there is something else that I may need to add to this other widget.js LayerList to listen to the TimeSlider and make sure they communicate with one another (?!) - perhaps   """""""""""this.fetch.data"""""""""""""" added to the startup function of the LayerList widget like you have suggested to someone else in another thread?

I just tried that as well but still.... it did not seem to make a difference....

Many, many thanks Robert!

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

Gabi,

    Historical Aerials will NOT be the actual id of the layer. It would look like Parcel Data_0.

You can retrieved the layer id from web map id as shown below. Where 32a83775654249dcae6b8f2eff5d4072 is your web map id.

http://<your portal url>/sharing/rest/content/items/32a83775654249dcae6b8f2eff5d4072/data/?f=pjson

View solution in original post

Reply
0 Kudos
Highlighted
New Contributor III

That WAS easy enough (when you know how..) - you really are the best - thank you, thank you !

Reply
0 Kudos
Highlighted
New Contributor III

Robert,

I try to use your code to turn on one layer in a map service. I used Screen Widget in WAB 2.7. After click on the report button I'd like to turn on one layer. Could you point out how to modify your code to post at here?

thanks

Zhujing

Reply
0 Kudos