API Javascript 4.13 - TileLayer - PopupTemplate

883
1
12-23-2019 05:40 AM
Olivier_MOUGEL
New Contributor II
Hi,
I try to acces to attribute data thru a tilelayer using a sublayer as written here https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-support-Sublayer.html#popu...
So my code is :
       var SA_Innondation = new TileLayer({
          title:"Sensibilité Aléas Inondation",
         sublayers: [{
    id: 0,
    popupTemplate: {
      title: "{ppr_label}",
      content: "{risq_2015}"
    }
  }]
        });
My console return this error :
Uncaught TypeError: [accessor] cannot assign to read-only property 'sublayers' of esri.layers.TileLayer
 
I declare :
 
      "esri/layers/TileLayer",
      "esri/layers/support/Sublayer",
So is it impossible to retrieve attribute data using a cached layer with Tilelayer ?
It's works with a mapimagelayer but the display is too slow.
Some ideas ?
Thank's
Olivier
Tags (1)
0 Kudos
1 Reply
KristianEkenes
Esri Regular Contributor

Hi Olivier, 

The reason the code works in MapImageLayer and not TileLayer is because you're creating a dynamic sublayer in the constructor of the MapImageLayer. This is technically a new sublayer and a powerful capability of MapImageLayer.

However, TileLayer has sublayers that are readonly. So when you use the same snippet, you're actually attempting to create new sublayers within the constructor of the TileLayer. Since sublayers is a readonly property, you will see an error. However, you can still modify properties like popupTemplate, popupEnabled, and legendEnabled on existing sublayers. You just need to modify the code a little bit to get the proper sublayer. This should work in your app:

var SA_Innondation = new TileLayer({
  url:"https://sig.gironde.fr/arcgis/rest/services/APP/sa_inondation_v_1_0/MapServer/",
  title:"Sensibilité Aléas Inondation"  
});

var sublayer = SA_Innondation.findSublayerById(0);
sublayer.popupTemplate = {
  title: "{ppr_label}",
  content: "{risq_2015}"
};‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍