How to access the featurelayer with sublayer

998
5
Jump to solution
10-07-2021 11:36 AM
RaymondLi
New Contributor III

I have the webmap layers as below

Layers


  • Hospitals
  • School

In the swift, I am able to access the first four layer, but I don't know how to access the Hospitals and School. My code as below

 

 

self.featureLayer = map.operationalLayers.firstObject as? AGSFeatureLayer
for layer in map.operationalLayers {
   if let fLayer = layer as? AGSFeatureLayer {
      print(fLayer.name)
   }else{
      if let subItems = layer as? AGSArcGISMapImageLayer{
         print(subItems.name)

      }else{
         print(layer)
      }
    }
}

 

 

How to I access those sublayer feature in swift

0 Kudos
1 Solution

Accepted Solutions
Ting
by Esri Contributor
Esri Contributor

Hi Raymond,

  • There doesn't seem to be a School layer on your service

Browsing through the service you provided, I don't see a layer named School. Hence I'll only use the Hospitals sublayer, under EMS group layer as an example.

  • About sublayers

You can refer to the Change sublayer visibility sample and other related ones in the iOS Sample Viewer repo to see how sublayer works.

  • Here is a snippet to show the Hospitals sublayer from your service

You can get to the Hospitals sublayer from the top level layer, by filtering layer ID, name, etc.

 

let portal = AGSPortal(url: URL(string: "https://company.org/portal")!, loginRequired: false)
let portalItem = AGSPortalItem(portal: portal, itemID: "9e1caaa976f54063a7f954d10327ce7e")
let topLayer = AGSArcGISMapImageLayer(item: portalItem)
topLayer.load { [self] _ in
    let sublayers = topLayer.mapImageSublayers as! [AGSArcGISMapImageSublayer]
    let emsLayer = sublayers.first { $0.name == "EMS" }!
    let hospitalsLayer = (emsLayer.subLayerContents as! [AGSArcGISMapImageSublayer]).first { $0.name == "Hospitals" }
    sublayers.forEach { $0.isVisible = false }
    emsLayer.isVisible = true
    emsLayer.subLayerContents.forEach { $0.isVisible = false }
    hospitalsLayer?.isVisible = true
    map.operationalLayers.add(topLayer)
    if let extent = topLayer.fullExtent {
        mapView.setViewpoint(AGSViewpoint(targetExtent: extent))
        mapView.setViewpointScale(3.5e4)
    }
    print(emsLayer.subLayerContents.map(\.name))
}

 

And below is a screenshot for the snippet above.

 hospitals.png

  • Here is another snippet to access the Hospitals layer as a feature layer

Alternatively, if you don't want to deal with the hassle of managing sublayers, and only want to display the hospital feature layer itself, here is another snippet. By creating a feature layer directly from its underlying table's URL, you can add it separately.

 

let featureLayer = AGSFeatureLayer(featureTable: AGSServiceFeatureTable(url: URL(string: "https://company.org/server/rest/services/some/MapServer/2")!))
featureLayer.load { [self] error in
    map.operationalLayers.add(featureLayer)
    if let extent = featureLayer.fullExtent {
        mapView.setViewpoint(AGSViewpoint(targetExtent: extent))
        mapView.setViewpointScale(3.5e4)
    }
}

 

  •  Please be aware...

The layer has minScale set to 35,000. You'll need to zoom in to see its symbology.

 

Hope it helps! Please let me know if you have other questions.

View solution in original post

5 Replies
Ting
by Esri Contributor
Esri Contributor

Hi Raymond,

  • There doesn't seem to be a School layer on your service

Browsing through the service you provided, I don't see a layer named School. Hence I'll only use the Hospitals sublayer, under EMS group layer as an example.

  • About sublayers

You can refer to the Change sublayer visibility sample and other related ones in the iOS Sample Viewer repo to see how sublayer works.

  • Here is a snippet to show the Hospitals sublayer from your service

You can get to the Hospitals sublayer from the top level layer, by filtering layer ID, name, etc.

 

let portal = AGSPortal(url: URL(string: "https://company.org/portal")!, loginRequired: false)
let portalItem = AGSPortalItem(portal: portal, itemID: "9e1caaa976f54063a7f954d10327ce7e")
let topLayer = AGSArcGISMapImageLayer(item: portalItem)
topLayer.load { [self] _ in
    let sublayers = topLayer.mapImageSublayers as! [AGSArcGISMapImageSublayer]
    let emsLayer = sublayers.first { $0.name == "EMS" }!
    let hospitalsLayer = (emsLayer.subLayerContents as! [AGSArcGISMapImageSublayer]).first { $0.name == "Hospitals" }
    sublayers.forEach { $0.isVisible = false }
    emsLayer.isVisible = true
    emsLayer.subLayerContents.forEach { $0.isVisible = false }
    hospitalsLayer?.isVisible = true
    map.operationalLayers.add(topLayer)
    if let extent = topLayer.fullExtent {
        mapView.setViewpoint(AGSViewpoint(targetExtent: extent))
        mapView.setViewpointScale(3.5e4)
    }
    print(emsLayer.subLayerContents.map(\.name))
}

 

And below is a screenshot for the snippet above.

 hospitals.png

  • Here is another snippet to access the Hospitals layer as a feature layer

Alternatively, if you don't want to deal with the hassle of managing sublayers, and only want to display the hospital feature layer itself, here is another snippet. By creating a feature layer directly from its underlying table's URL, you can add it separately.

 

let featureLayer = AGSFeatureLayer(featureTable: AGSServiceFeatureTable(url: URL(string: "https://company.org/server/rest/services/some/MapServer/2")!))
featureLayer.load { [self] error in
    map.operationalLayers.add(featureLayer)
    if let extent = featureLayer.fullExtent {
        mapView.setViewpoint(AGSViewpoint(targetExtent: extent))
        mapView.setViewpointScale(3.5e4)
    }
}

 

  •  Please be aware...

The layer has minScale set to 35,000. You'll need to zoom in to see its symbology.

 

Hope it helps! Please let me know if you have other questions.

RaymondLi
New Contributor III

Do you mind to remove the host name on your post since it is not suppose here

0 Kudos
Ting
by Esri Contributor
Esri Contributor

Yup removed.

0 Kudos
RaymondLi
New Contributor III

Hi Ting,

 

Is it possible to convert the AGSArcGISMapImageSublayer to AGSFeatureLayer? I need to do the show the popup for the featurelayers also?

0 Kudos
Ting
by Esri Contributor
Esri Contributor

Is it possible to convert the AGSArcGISMapImageSublayer to AGSFeatureLayer? 


No it is not possible. Image layers, as its name indicates, are images rendered by the map server and transferred to the client app. They are read-only and better suitable for displaying purposes. You'll need to create each feature layer from their service feature table in order to work with the features.

Please let me know if you have other questions.

0 Kudos