Listener for layer toggle widget

5756
68
Jump to solution
07-20-2018 07:37 AM
MarkLache1
Occasional Contributor

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
RobertScheitlin__GISP
MVP Emeritus

Mark,

   Strange back in this post of your you were showing that the layer was not undefined:

https://community.esri.com/message/794280-re-listener-for-layer-toggle-widget?commentID=794280#comme... 

0 Kudos
MarkLache1
Occasional Contributor

Very strange. I could see the listener working as I switched between the layers each button click (initially in the console). Nothing has changed on my end in terms of services or the layers themselves. These layers are hosted on AGOL without any weird parameters.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Mark,

  So now the layer object is always null when you toggle a layer?

0 Kudos
MarkLache1
Occasional Contributor

Fixed it! Was missing the declaration for LayerStructure.

The SERVICE_ID is returning the layer name as expected, but its also retuning a number after the layer name. Also, how can a split function be used on this.SERVICE_ID?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Just like you would any other string.

0 Kudos
MarkLache1
Occasional Contributor

Everything else works but now I get undefined when I switch to my 2nd layer

  _publishMapEvent: function(map) {
        topic.subscribe('toggleChanged', lang.hitch(this, function(visible, layer){
          if(visible){
            this.SERVICE_ID = layer.id;
          }
        }));

        //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);
        }

        require([
          'esri/graphic',
          'esri/symbols/SimpleMarkerSymbol',
          'esri/symbols/SimpleLineSymbol',
          'esri/Color'
        ], lang.hitch(this, function(Graphic, SimpleMarkerSymbol, SimpleLineSymbol, Color){
          var symbol = new SimpleMarkerSymbol(
          SimpleMarkerSymbol.STYLE_CIRCLE,
          12,
          new SimpleLineSymbol(
            SimpleLineSymbol.STYLE_NULL,
            new Color([247, 34, 101, 0.9]),
            1
          ),
          new Color([207, 34, 171, 0.5])
        );
          map.on("click", lang.hitch(this, function(evt){
            var gra = new Graphic(evt.mapPoint, symbol);
            setTimeout(lang.hitch(this, function(){
              var selFeats = map.infoWindow.features;
              if(!selFeats){
                map.graphics.clear();
                map.graphics.add(gra);
                    
                map.infoWindow.setContent('<a href="https://survey123.arcgis.com/share/5f16c4f6ce6f42aebf09e145eabe5674?center='+ evt.mapPoint.getLatitude().toString() + ','+ evt.mapPoint.getLongitude().toString() + '&field:Floor_Selection='+ this.SERVICE_ID.split('$')[1]  + '" target="_blank"><font size="4">Click here to submit a service request</font></a>');
                    
                    
                map.infoWindow.show(evt.mapPoint);
              }
            }), 1000);
          }));
        }));
      },
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Mark,

  Are you sure that your second layer ID has a '$' in it?

0 Kudos
MarkLache1
Occasional Contributor

Used layer.title instead. Got it all working Thanks!

0 Kudos
MarkLache1
Occasional Contributor

If I have more than 2 layers in the layer toggle the result that is spit out "undefined". I only need the same 2 of the 6 layers to be examined for toggle change.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Mark,

   I have 4 toggle layers in my app and I do not see that issue. My situation is that the 4 layers are part of the same Map Service though. What is your scenario?

0 Kudos