Get FeatureLayer from Map

5160
9
Jump to solution
07-26-2018 11:04 AM
PaulGiard
New Contributor III

I am a little stuck here and I hope someone can point me to the right place.  

Using the ArcGIS-js-api in Angular / Typescript

I am able to display a WebMap via an internal Portal instance.

I can access the Layers that the Map is composed of but I am unable to get the layer as a featureLayer.

All of the examples I see are adding a featureLayer to the map.

How can I get a featureLayer from the Map?

0 Kudos
1 Solution

Accepted Solutions
PaulGiard
New Contributor III

It looks like it is a problem with Typings.  The code works when casting directly:

const featureLayer: esri.FeatureLayer = this.mapView.map.findLayerById('69bf94b82f1a4ff8b7afcf370b8c00db') as esri.FeatureLayer;

View solution in original post

0 Kudos
9 Replies
RobertScheitlin__GISP
MVP Emeritus

Paul,

   If you are taking 3.x then it is as simple as:

var incidentLayer = map.getLayer("incidentLayer");

This assumes you know the id of the layer.

0 Kudos
PaulGiard
New Contributor III

Thanks for the response,

I am using 4.8 and there is no getLayer method on the Map

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Paul,

   Be sure to add a tag to your post or some text stating the API version your are targeting.

So in 4.x when using a WebMap you just use the WebMaps findLayerById

https://developers.arcgis.com/javascript/latest/api-reference/esri-WebMap.html#findLayerById 

0 Kudos
PaulGiard
New Contributor III

I did try that and I get the same issue as when I loop through the Layer Collection

FindLayerById returns a Layer and I don't know how to cast to a FeatureLayer

Type 'Layer' is not assignable to type 'FeatureLayer'.
Property 'capabilities' is missing in type 'Layer'.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Paul,

   Is it really a FeatureLayer in the Web Map then? Are you sure it is not a Whole MapService and thus a MapImageLayer?

0 Kudos
PaulGiard
New Contributor III

When I loop through the layers the type is feature

Our C# code on Xamarin with the .Net API autocasts the same layer as expected.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Paul,

   If that is the case then when you use findLayerById then the returned layer will already be a FeatureLayer class.

0 Kudos
diaconori
New Contributor III

Isn't there a way to do this without providing the value through a hard-coded string? 

Something like:

var layers = this.map.getLayers();
// process layers in a for-each loop...
0 Kudos
PaulGiard
New Contributor III

It looks like it is a problem with Typings.  The code works when casting directly:

const featureLayer: esri.FeatureLayer = this.mapView.map.findLayerById('69bf94b82f1a4ff8b7afcf370b8c00db') as esri.FeatureLayer;
0 Kudos