Change sublayer visibility

View on GitHubSample viewer app

Change the visibility of sublayers.

All sublayers visible Cities sublayer hidden )

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 hide certain sublayers by changing their visibility.

How to use the sample

Tap the bottom button to display a list of sublayers. Tap the switches to toggle the visibility of the sublayers.

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 layer in the sublayer list, set its visible property to true or false.

Relevant API

  • AGSArcGISMapImageLayer
  • AGSArcGISMapImageSublayer

Tags

layer, sublayer, visibility

Sample Code

SublayerVisibilityViewController.swiftSublayerVisibilityViewController.swiftSublayersTableViewController.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
// Copyright 2016 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 SublayerVisibilityViewController: UIViewController {
    @IBOutlet private weak var mapView: AGSMapView!

    private var map: AGSMap!
    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 = ["SublayerVisibilityViewController", "SublayersTableViewController"]

        // initialize map with topographic basemap
        self.map = AGSMap(basemapStyle: .arcGISTopographic)

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

        // add the image layer to the map
        self.map.operationalLayers.add(mapImageLayer)

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

        // zoom to a custom viewpoint
        self.mapView.setViewpointCenter(AGSPoint(x: -11e6, y: 6e6, spatialReference: .webMercator()), scale: 9e7)

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

    // MARK: - Navigation

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "SublayersPopover" {
            // get the destination view controller as BookmarksListViewController
            let controller = segue.destination as! SublayersTableViewController
            if let sublayers = mapImageLayer.mapImageSublayers as? [AGSArcGISMapImageSublayer] {
                controller.sublayers = sublayers
            }
            controller.presentationController?.delegate = self
        }
    }
}

extension SublayerVisibilityViewController: UIAdaptivePresentationControllerDelegate {
    func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
        return .none
    }
}

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