Feature Layer with sublayers

5521
9
Jump to solution
08-16-2017 09:06 AM
GregSalvador
New Contributor III

Hi all!

I have several layers I want to add to a map. Each layer has sublayers and an attribute table. When I was first practicing with adding layers, I made them all MapImageLayers so I could see the sublayers. Now I want to add popups that display the information in the attribute table. But when I made the layers all FeatureLayers, all of my sublayers disappeared! The FeatureLayer documentation doesn't mention children or sublayers, so I'm not sure how to get the sublayers to show back up in my LayerList widget. Help me ArcGISPros-wan-Kenobi, you're my only hope!

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Greg,

   No there is not, so it is back to a MapImageLayer for you then. If you want the MapService to be one layer in the layer list with sublayers as children then you need to use MapImageLayer. So back to the question of your original intent/need. If you need to specify a popup for a sublayer of a MapImageLayer then you just assign the sublayer a specific popup.

// Overrides the drawing info on the layer with a custom renderer
var citiesLayer = layer.sublayers.getItemAt(3);
citiesLayer.popupTemplate = new PopupTemplate({
  title: "Population in {NAME}",
  content: "As of 2010, the population in this area was <b>{POP2010:NumberFormat}</b>." +
           "As of 2013, the population here was <b>{POP2013:NumberFormat}</b>." +
           "Percent change is {POP2013:populationChange}"
});

View solution in original post

9 Replies
RobertScheitlin__GISP
MVP Emeritus

Greg,

   A FeatureLayer is a specific layer inside a Map Service. So the url would include the specific layer ID in the MapService.

https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/0

0 Kudos
GregSalvador
New Contributor III

Thanks for reply Robert, it worked great. However, in my layerList widget, the FeatureLayer shows up as a separate layer. This makes sense, because I added it to the Map the same way I added all of the MapImageLayeres. I would like to replace the sublayer with the FeatureLayer, not make it seperate from its parent layer. Is there a way to do this?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Greg,

   No there is not, so it is back to a MapImageLayer for you then. If you want the MapService to be one layer in the layer list with sublayers as children then you need to use MapImageLayer. So back to the question of your original intent/need. If you need to specify a popup for a sublayer of a MapImageLayer then you just assign the sublayer a specific popup.

// Overrides the drawing info on the layer with a custom renderer
var citiesLayer = layer.sublayers.getItemAt(3);
citiesLayer.popupTemplate = new PopupTemplate({
  title: "Population in {NAME}",
  content: "As of 2010, the population in this area was <b>{POP2010:NumberFormat}</b>." +
           "As of 2013, the population here was <b>{POP2013:NumberFormat}</b>." +
           "Percent change is {POP2013:populationChange}"
});
GregSalvador
New Contributor III

Thanks Robert, this is exactly what I was looking for! This code makes a lot of sense.

Small problem, though: layer.sublayers comes back empty. This is what I have

var BREADCADLayer = landLayer.sublayers.getItemAt(40);

with landLayer being a MapImageLayer that i know has sublayers. After removing the getItemAt() function and putting landLayer.sublayers.join() in a console.log, I confirmed that the collection is empty. Am i missing something here?

edit: I don't specify the sublayers property of landLayer in its constructor, as I want every sublayer in the MapService to be included. The only thing specified in the landLayer constructor is url.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Greg,

   What are you doing ion your code to ensure that the layer is actually loaded before executing your code?

0 Kudos
GregSalvador
New Contributor III

umm, nothing really. I know the layer is loading because I can access it in layerList widget and it has sublayers. I put your code snippit at the bottom of my script, but I don't know if that guarantees that snippit gets executed last

edit: I should put your code snippit in something like view.then(snippit), so it load after everything is loaded?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Greg,

   Well the layer list is going to wait for the layer to load before working with the sublsyers and you need to do the same. So you then do:

var BREADCADLayer = new MapImageLayer(...);
BREADCADLayer.then(function(fl){
  //now the sublayers should not be empty
});‍‍‍‍
GregSalvador
New Contributor III

Very cool! I certainly learned a lot here, thank you very much

0 Kudos
BjørnarSundsbø1
Occasional Contributor II

I also have this problem. While it makes sense there is a one-to-one layer mapping, is there an easy way to discover available layers in the MapServer, and dynamically add those layers to the map, rather than having to manually add layer 0, 1 and 2? I don't mind requesting layer list from the server, and then iterating them, adding them to group layer or directly to the map; I just don't know how to query the ArcGIS MapServer for available layers. Since I get the url from a configuration, I'd rather avoid having to specify configuration per layer. 

Resolution: Using a dynamic layer behind the scenes, I can iterate the Layers collection to create FeatureLayer for the layers in the MapServer, adding them to the map, while discarding the dynamic layer.