I have the webmap layers as below
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
Solved! Go to Solution.
Hi Raymond,
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.
You can refer to the Change sublayer visibility sample and other related ones in the iOS Sample Viewer repo to see how sublayer works.
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.
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)
}
}
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.
Hi Raymond,
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.
You can refer to the Change sublayer visibility sample and other related ones in the iOS Sample Viewer repo to see how sublayer works.
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.
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)
}
}
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.
Do you mind to remove the host name on your post since it is not suppose here
Yup removed.
Hi Ting,
Is it possible to convert the AGSArcGISMapImageSublayer to AGSFeatureLayer? I need to do the show the popup for the featurelayers also?
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.