Sublayer createFeatureLayer()

593
7
04-10-2019 09:14 AM
MatthieuThery1
Occasional Contributor

Hello,

I am trying to create a feature layer from a MapImageLayer sublayer and it's not working.

Using the code snippet from the API (4.11) doc:

var sublayer = mapImageLayer.findSublayerById(0); // here it is finding the sublayer

sublayer.createFeatureLayer().then(function(featureLayer){ return featureLayer.load(); })

The error I am getting is :

Uncaught (in promise) TypeError: sublayer.createFeatureLayer(...).then is not a function

Any idea ?

Tags (2)
0 Kudos
7 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor

Matthieu,

  And you are sure the mapImageLayer.findSublayerById(0) did return a Sublayer class?

0 Kudos
MatthieuThery1
Occasional Contributor

Yes I checked. I am able to turn the visibility on/off etc.

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Is it from a dynamic workspace or just a normal MapService layer?

0 Kudos
Noah-Sager
Esri Regular Contributor

I tried with a dynamic workspace and a normal MapService, and they both seem to work. Here's a simple sample using the same code snippet from the API Reference:

https://codepen.io/noash/pen/JVWVzZ

It sounds like there is something going on with the value of sublayer (perhaps it is being overwritten? typo somewhere?) as this is a valid method as of 4.7.

0 Kudos
MatthieuThery1
Occasional Contributor

Right, I must be doing something wrong.

I tried that method:

export const test = () => {
    return esriPromise(['esri/layers/MapImageLayer']).then(([ MapImageLayer ]) => {
 var layer = new MapImageLayer({
url: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer",
 sublayers: [ {id: 2,visible: false},{id: 1,visible: false},{id: 0,visible: false}]});

map.layers.add(layer);
 var sublayer= layer.findSublayerById(0);
 console.log("sublayer: ", sublayer);
 
 // when the createFeatureLayer() promise resolves, load the FeatureLayer
 // and pass it to the createFeatureLayer function
 sublayer.createFeatureLayer()
 .then((featureLayer) => {
 console.log("blah")
 return featureLayer.load();
 })
 .then(createFeatureLayer);
 
 function createFeatureLayer (featureLayer) {
 featureLayer.visible = true;
 map.add(featureLayer);
 console.log("featureLayer: ", featureLayer);
 }})
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
And I am still getting the same error:
Now you can see I am using @esri/react-arcgis, could this be the issue (never had a problem before). Or am I doing something obviously wrong, that I can't see.
0 Kudos
MatthieuThery1
Occasional Contributor

and now if I do that it works:

var x = new Promise((resolve) => {
 resolve(sublayer.createFeatureLayer()) 
 })
 .then((featureLayer) => {
 console.log("blah")
 return featureLayer.load();
 })‍‍‍‍‍‍‍

My bad.

0 Kudos
ReneRubalcava
Frequent Contributor II

Are you able to share the URL of the map service for testing?

0 Kudos