Listener for layer toggle widget

4881
68
Jump to solution
07-20-2018 07:37 AM
MarkLache1
New Contributor III

How would I add a listener to Robert's layer toggle widget that would be able to tell what layer is on? I want this so that the info can be passed into Survey123 via custom URL

Thanks

0 Kudos
68 Replies
MarkLache1
New Contributor III

All I really need to do is capture the layer name that is currently on in a text string. Something like: 

topic.subscribe('toggleChanged', function()
  {
   var URL =….;
 
}
Or just a way to identify one layer from another. Even having an ID of 1 when the button is off and an ID of 2 when the toggle button is turned on.
Suggestions?
Thanks
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Mark,

   Something like:

topic.subscribe('toggleChanged', function(event){ 
   console.info(event);
//Now that you can see the contents of the event in your browsers console
//you will know what to add to your url.
   var URL ='…';
}
0 Kudos
MarkLache1
New Contributor III

I've tried pushing this to the console and I keep getting "Cross-Origin Request Blocked" errors. I'm using publishMapEvent function

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Mark,

   Show me all the code from your modified publishMapEvent function.

0 Kudos
MarkLache1
New Contributor III
_publishMapEvent: function(map) {
        //add this property for debug purpose
        window._viewerMap = map;

        MapUrlParamsHandler.postProcessUrlParams(this.urlParams, map);

                    
     
     topic.subscribe('toggleChanged', function(event){ 
               
          console.log(event)

   
             }
          )
     
                    
        console.timeEnd('Load Map');
        if (this.map) {
          this.map = map;
          this.resetInfoWindow(true);
          console.log('map changed.');
          topic.publish('mapChanged', this.map, this.layerInfosObj);
        } else 
          {
               
          this.map = map;
          this.resetInfoWindow(true);
          topic.publish('mapLoaded', this.map, this.layerInfosObj);
        }
      },
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Mark,

   That change would not produce a cross origin error.

Here is what I have working fine in that exact function:

      _publishMapEvent: function(map) {
        topic.subscribe('toggleChanged', function(visible, layer){
           console.info(visible, layer);
        });

        //add this property for debug purpose
        window._viewerMap = map;

        MapUrlParamsHandler.postProcessUrlParams(this.urlParams, map);

        console.timeEnd('Load Map');
        if (this.map) {
          this.map = map;
          this.resetInfoWindow(true);
          console.log('map changed.');
          topic.publish('mapChanged', this.map, this.layerInfosObj);
        } else {
          this.map = map;
          this.resetInfoWindow(true);
          topic.publish('mapLoaded', this.map, this.layerInfosObj);
        }
      },
0 Kudos
MarkLache1
New Contributor III

Get the same error "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at..." but only when I click the layer toggle widget. Get this error twice

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Mark,

  I have no idea where that error is coming from. Does it prevent your app from working?

0 Kudos
MarkLache1
New Contributor III

App still works fine. Just need this to identify if the toggle is on or off.

0 Kudos
MarkLache1
New Contributor III

Don't know if it makes a difference but I am running it thru local host WAB

0 Kudos