Add, remove, or rearrange existing sublayers in a map image layer.
Use case
A map image layer may contain many sublayers such as different types of roads in a road network or city, county, and state borders in a US map. The user may only be interested in a subset of these sublayers. Or, perhaps showing all of the sublayers would show too much detail. In these cases, you can add, remove, or rearrange the order of the sublayers.
How to use the sample
Tap the bottom button to display a list of sublayers. Tap the red button to remove a layer. Tap the green button to add a layer. Tap and drag the the right of a cell to rearrange the order of the layers.
How it works
Create an AGSArcGISMapImageLayer object with the URL to a map image service.
Get all AGSArcGISMapImageSublayers of the map image layer.
For each corresponding layer, add to, remove from, or rearrange the array of sublayers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//// Copyright 2017 Esri.//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at//// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.import UIKit
import ArcGIS
classManageSublayersViewController: UIViewController, MapImageSublayersViewControllerDelegate{
@IBOutletprivatevar mapView: AGSMapView!
privatevar workspaceID ="MyDatabaseWorkspaceIDSSR2"privatevar removedMapImageSublayers = [AGSArcGISMapImageSublayer]()
privatevar mapImageLayer: AGSArcGISMapImageLayer!
overridefuncviewDidLoad() {
super.viewDidLoad()
// add the source code button item to the right of navigation bar (self.navigationItem.rightBarButtonItem as!SourceCodeBarButtonItem).filenames = ["ManageSublayersViewController", "MapImageSublayersViewController"]
// instantiate map with basemaplet map =AGSMap(basemapStyle: .arcGISStreets)
// initialize map image layerlet mapImageLayer =AGSArcGISMapImageLayer(url: URL(string: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer")!)
// add layer to map map.operationalLayers.add(mapImageLayer)
// initial viewpointlet envelope =AGSEnvelope(xMin: -13834661.666904, yMin: 331181.323482, xMax: -8255704.998713, yMax: 9118038.075882, spatialReference: .webMercator())
// assign map to map viewself.mapView.map = map
// set the viewpoint on map viewself.mapView.setViewpoint(AGSViewpoint(targetExtent: envelope))
// create sublayers from tableSublayerSourceself.createSublayers()
// store the map image layer for later useself.mapImageLayer = mapImageLayer
}
privatefunccreateSublayers() {
// We will create 2 mapImageSublayers from tableSublayerSource with known workspaceID and dataSourceName// These sublayers are not yet part of the mapImageLayer's sublayers, so will be shown as part of the// removed sublayers array at first// create tableSublayerSource from workspaceID and dataSourceNamelet tableSublayerSource1 =AGSTableSublayerSource(workspaceID: self.workspaceID, dataSourceName: "ss6.gdb.rivers")
// create mapImageSublayer from tableSublayerSourcelet mapImageSublayer1 =AGSArcGISMapImageSublayer(id: 4, source: tableSublayerSource1)
// assign a renderer to the sublayerlet renderer1 =AGSSimpleRenderer(symbol: AGSSimpleLineSymbol(style: .solid, color: .blue, width: 1))
mapImageSublayer1.renderer = renderer1
// name for the sublayer mapImageSublayer1.name ="Rivers"// create tableSublayerSource from workspaceID and dataSourceNamelet tableSublayerSource2 =AGSTableSublayerSource(workspaceID: self.workspaceID, dataSourceName: "ss6.gdb.lakes")
// create mapImageSublayer from tableSublayerSourcelet mapImageSublayer2 =AGSArcGISMapImageSublayer(id: 5, source: tableSublayerSource2)
// assign a renderer to the sublayerlet renderer2 =AGSSimpleRenderer(symbol: AGSSimpleFillSymbol(style: .solid, color: .cyan, outline: nil))
mapImageSublayer2.renderer = renderer2
// name for the sublayer mapImageSublayer2.name ="Lakes" removedMapImageSublayers.append(contentsOf: [mapImageSublayer1, mapImageSublayer2])
}
// MARK: - MapImageSublayersViewControllerDelegatefuncmapImageSublayersViewController(_controller: MapImageSublayersViewController, didCloseWithremovedMapImageSublayers: [AGSArcGISMapImageSublayer]) {
self.removedMapImageSublayers = removedMapImageSublayers
}
// MARK: - Navigationoverridefuncprepare(forsegue: UIStoryboardSegue, sender: Any?) {
if segue.identifier =="MapImageSublayersSegue",
let navigationController = segue.destination as?UINavigationController,
let controller = navigationController.viewControllers.first as?MapImageSublayersViewController {
controller.delegate =self controller.preferredContentSize =CGSize(width: 300, height: 300)
controller.mapImageLayer =self.mapImageLayer
controller.removedMapImageSublayers =self.removedMapImageSublayers
}
}
}