Manage sublayers

View on GitHubSample viewer app

Add, remove, or rearrange existing sublayers in a map image layer.

Map displaying all sublayers Sublayer settings

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

  1. Create an AGSArcGISMapImageLayer object with the URL to a map image service.
  2. Get all AGSArcGISMapImageSublayers of the map image layer.
  3. For each corresponding layer, add to, remove from, or rearrange the array of sublayers.

Relevant API

  • AGSArcGISMapImageLayer
  • AGSArcGISMapImageSublayer

Tags

layer, sublayer, visibility

Sample Code

ManageSublayersViewController.swiftManageSublayersViewController.swiftMapImageSublayersViewController.swift
Use dark colors for code blocksCopy
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

class ManageSublayersViewController: UIViewController, MapImageSublayersViewControllerDelegate {
    @IBOutlet private var mapView: AGSMapView!

    private var workspaceID = "MyDatabaseWorkspaceIDSSR2"
    private var removedMapImageSublayers = [AGSArcGISMapImageSublayer]()
    private var mapImageLayer: AGSArcGISMapImageLayer!

    override func viewDidLoad() {
        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 basemap
        let map = AGSMap(basemapStyle: .arcGISStreets)

        // initialize map image layer
        let mapImageLayer = AGSArcGISMapImageLayer(url: URL(string: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer")!)

        // add layer to map
        map.operationalLayers.add(mapImageLayer)

        // initial viewpoint
        let envelope = AGSEnvelope(xMin: -13834661.666904, yMin: 331181.323482, xMax: -8255704.998713, yMax: 9118038.075882, spatialReference: .webMercator())

        // assign map to map view
        self.mapView.map = map

        // set the viewpoint on map view
        self.mapView.setViewpoint(AGSViewpoint(targetExtent: envelope))

        // create sublayers from tableSublayerSource
        self.createSublayers()

        // store the map image layer for later use
        self.mapImageLayer = mapImageLayer
    }

    private func createSublayers() {
        // 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 dataSourceName
        let tableSublayerSource1 = AGSTableSublayerSource(workspaceID: self.workspaceID, dataSourceName: "ss6.gdb.rivers")

        // create mapImageSublayer from tableSublayerSource
        let mapImageSublayer1 = AGSArcGISMapImageSublayer(id: 4, source: tableSublayerSource1)

        // assign a renderer to the sublayer
        let 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 dataSourceName
        let tableSublayerSource2 = AGSTableSublayerSource(workspaceID: self.workspaceID, dataSourceName: "ss6.gdb.lakes")

        // create mapImageSublayer from tableSublayerSource
        let mapImageSublayer2 = AGSArcGISMapImageSublayer(id: 5, source: tableSublayerSource2)

        // assign a renderer to the sublayer
        let 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: - MapImageSublayersViewControllerDelegate

    func mapImageSublayersViewController(_ controller: MapImageSublayersViewController, didCloseWith removedMapImageSublayers: [AGSArcGISMapImageSublayer]) {
        self.removedMapImageSublayers = removedMapImageSublayers
    }

    // MARK: - Navigation

    override func prepare(for segue: 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
        }
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.