Trouble accessing feature layer in WAB (DE)

662
6
Jump to solution
05-17-2019 09:38 AM
JoelPerkins
New Contributor III

I'm having a little trouble accessing my feature layer in the Web Appbuilder app. If I did the following outside of WAB, just in a testing JS 3.x environment, it would work. 

<script>

 var myFL = new FeatureLayer("https://blahblah/MapServer/6", {
 mode: FeatureLayer.MODE_AUTO,
 outFields: "
  • ",
     id: "myParcels"
     })
     map.addLayer(myFL);

     myFL.on("click", function (evt) {
     var geom = evt.graphic.geometry;
     console.log(geom)

    </script>

    But in WAB, myFL already exists in there, so if I do map.addLayer(myFL), it's adding it a second time, but that's the only way I can successfully return anything on a click event. 

    If I do the following in WAB, not adding the layer (because it's already in the map and I don't want it twice), I can't return anything on the click event. I'm not sure what I'm missing that I can't return anything?? I feel like I've tried everything to access this layer, I've tried map.getLayer(id), and that didn't work. Not sure why it isn't working...

    <script>

    {
     return declare(BaseWidget, {
    //Derive from BaseWidget.
      baseClass: 'jimu-widget-myWidget',
      isActive: true,
      startup: function() {
      this.inherited(arguments);
      this.myFL = new FeatureLayer('https://blahblah/MapServer/6',{
      outFields: ["*"]})
    },
      onOpen: function(){
      this.myFL.on('click', function(evt){
      var geom = evt.graphic.geometry;
      console.log(geom)
      })
     } 

     });
     });

    </script>

    0 Kudos
    1 Solution

    Accepted Solutions
    RobertScheitlin__GISP
    MVP Emeritus

    Joel,

    ConstructionSvcs_scratch_6492_6 means that the layer is a sub layer of a ArcGISDynamicMapServiceLayer so though the layer is added to the map an ArcGISDynamicMapServiceLayer sublayer is not an actual layer that can have a click event. An ArcGISDynamicMapServiceLayer is just an image returned from the server and thus no actual geometry is returned to the client that could be used for a click event.  You need to add the layer as a Feature layer so that the client is getting actual geometries returned to it.

    View solution in original post

    0 Kudos
    6 Replies
    RobertScheitlin__GISP
    MVP Emeritus

    Joel,

      If the layer is already added to the map then you just need to get a reference to the layer and add your on click method to the existing layer in the map.

    var myfl = this.map.getLayer("your layer id");

    0 Kudos
    JoelPerkins
    New Contributor III

    Thanks Robert, 

    I was trying that before, the only thing I can think of is that I'm not getting the right ID. 

    in getLayer(ID), would it be "6" here? or "Taxlots"? Neither work, but I want to make sure I'm at least getting the right one. Does it matter that it's in a group layer, and it's a child of Landbase?

    0 Kudos
    RobertScheitlin__GISP
    MVP Emeritus

    Joel,

       Nope neither of those are the id as you have seen. The easyest way to find the layers ID is to open the layerlist widget and right click on the layer in your layerlist and choose inspect or inspect element. It will look something like this:

    <div class="layer-title-div-ParcelViewerWM_1607 div-content jimu-float-leading ">Parcel Data</div>

    The layers id is ParcelViewerWM_1607

    0 Kudos
    JoelPerkins
    New Contributor III

    Ok thanks, 

    Just so I'm not totally going insane here, looking at my code, is there any thing that jumps out at you with the onOpen method?

    I keep getting this error, and I don't understand why. 

    TypeError: Unable to get property 'on' of undefined or null reference

      <script> 

       
         define(['dojo/_base/declare', 'jimu/BaseWidget','dojo/on',"dojo/domReady!"],
         function(declare, BaseWidget, on) {

        return declare([BaseWidget], {

        baseClass: 'jimu-widget-myWidget',

        onOpen: function(){
        myFL = this.map.getLayer("ConstructionSvcs_scratch_6492_6");
        myFL.on("click", function(e){
            console.log(e)
        })
       },

       });
       });

      </script>

    0 Kudos
    RobertScheitlin__GISP
    MVP Emeritus

    Joel,

    ConstructionSvcs_scratch_6492_6 means that the layer is a sub layer of a ArcGISDynamicMapServiceLayer so though the layer is added to the map an ArcGISDynamicMapServiceLayer sublayer is not an actual layer that can have a click event. An ArcGISDynamicMapServiceLayer is just an image returned from the server and thus no actual geometry is returned to the client that could be used for a click event.  You need to add the layer as a Feature layer so that the client is getting actual geometries returned to it.

    0 Kudos
    JoelPerkins
    New Contributor III

    OK, that was the issue. Thanks Robert!!

    0 Kudos