|
| 1 | +// Copyright 2025 Esri |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import ArcGIS |
| 16 | +import SwiftUI |
| 17 | + |
| 18 | +struct ApplyClassBreaksRendererToSublayerView: View { |
| 19 | + /// The map image layer. |
| 20 | + @State private var mapImageLayer = ArcGISMapImageLayer(url: URL(string: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer")!) |
| 21 | + |
| 22 | + /// The counties sublayer. |
| 23 | + @State private var countiesSublayer: ArcGISMapImageSublayer? |
| 24 | + |
| 25 | + /// The original renderer used to symbolize the sublayer. |
| 26 | + @State private var originalRenderer: Renderer? |
| 27 | + |
| 28 | + /// A Boolean value indicating that the class breaks renderer is applied. |
| 29 | + @State private var classBreaksRendererIsApplied = false |
| 30 | + |
| 31 | + /// The map with a topographic basemap style. |
| 32 | + @State private var map: Map = { |
| 33 | + let map = Map(basemapStyle: .arcGISTopographic) |
| 34 | + |
| 35 | + // Sets the initial viewpoint. |
| 36 | + map.initialViewpoint = Viewpoint( |
| 37 | + boundingGeometry: Envelope( |
| 38 | + xMin: -13934661.666904, |
| 39 | + yMin: 331181.323482, |
| 40 | + xMax: -7355704.998713, |
| 41 | + yMax: 9118038.075882, |
| 42 | + spatialReference: .webMercator |
| 43 | + ) |
| 44 | + ) |
| 45 | + return map |
| 46 | + }() |
| 47 | + |
| 48 | + /// The error shown in the error alert. |
| 49 | + @State private var error: Error? |
| 50 | + |
| 51 | + var body: some View { |
| 52 | + MapView(map: map) |
| 53 | + .task { |
| 54 | + // Adds the map image layer to the map. |
| 55 | + map.addOperationalLayer(mapImageLayer) |
| 56 | + do { |
| 57 | + // Loads the map image layer. |
| 58 | + try await mapImageLayer.load() |
| 59 | + // Gets the sublayers. |
| 60 | + let mapImageSublayers = mapImageLayer.mapImageSublayers |
| 61 | + // Gets the third sublayer. |
| 62 | + let sublayer = mapImageSublayers[2] |
| 63 | + // Loads the sublayer. |
| 64 | + try await sublayer.load() |
| 65 | + countiesSublayer = sublayer |
| 66 | + originalRenderer = sublayer.renderer |
| 67 | + } catch { |
| 68 | + self.error = error |
| 69 | + } |
| 70 | + } |
| 71 | + .toolbar { |
| 72 | + ToolbarItem(placement: .bottomBar) { |
| 73 | + Toggle("Change Sublayer Renderer", isOn: $classBreaksRendererIsApplied) |
| 74 | + .disabled(countiesSublayer == nil) |
| 75 | + } |
| 76 | + } |
| 77 | + .onChange(of: classBreaksRendererIsApplied) { |
| 78 | + countiesSublayer?.renderer = if classBreaksRendererIsApplied { |
| 79 | + // Applies the class breaks renderer. |
| 80 | + .populationRenderer |
| 81 | + } else { |
| 82 | + // Applies the original renderer. |
| 83 | + originalRenderer |
| 84 | + } |
| 85 | + } |
| 86 | + .errorAlert(presentingError: $error) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +private extension Renderer { |
| 91 | + /// Creates a class breaks renderer for counties in the US based on their |
| 92 | + /// population in 2007. |
| 93 | + static var populationRenderer: ClassBreaksRenderer { |
| 94 | + // Outline symbol. |
| 95 | + let lineSymbol = SimpleLineSymbol(style: .solid, color: UIColor(white: 0.6, alpha: 1), width: 0.5) |
| 96 | + |
| 97 | + // Symbol for each class break. |
| 98 | + let symbol1 = SimpleFillSymbol(style: .solid, color: UIColor(red: 0.89, green: 0.92, blue: 0.81, alpha: 1), outline: lineSymbol) |
| 99 | + let symbol2 = SimpleFillSymbol(style: .solid, color: UIColor(red: 0.59, green: 0.76, blue: 0.75, alpha: 1), outline: lineSymbol) |
| 100 | + let symbol3 = SimpleFillSymbol(style: .solid, color: UIColor(red: 0.38, green: 0.65, blue: 0.71, alpha: 1), outline: lineSymbol) |
| 101 | + let symbol4 = SimpleFillSymbol(style: .solid, color: UIColor(red: 0.27, green: 0.49, blue: 0.59, alpha: 1), outline: lineSymbol) |
| 102 | + let symbol5 = SimpleFillSymbol(style: .solid, color: UIColor(red: 0.16, green: 0.33, blue: 0.47, alpha: 1), outline: lineSymbol) |
| 103 | + |
| 104 | + // Class breaks. |
| 105 | + let classBreak1 = ClassBreak(description: "-99 to 8,560", label: "-99 to 8,560", minValue: -99, maxValue: 8_560, symbol: symbol1) |
| 106 | + let classBreak2 = ClassBreak(description: "> 8,560 to 18,109", label: "> 8,560 to 18,109", minValue: 8_561, maxValue: 18_109, symbol: symbol2) |
| 107 | + let classBreak3 = ClassBreak(description: "> 18,109 to 35,501", label: "> 18,109 to 35,501", minValue: 18_110, maxValue: 35_501, symbol: symbol3) |
| 108 | + let classBreak4 = ClassBreak(description: "> 35,501 to 86,100", label: "> 35,501 to 86,100", minValue: 35_502, maxValue: 86_100, symbol: symbol4) |
| 109 | + let classBreak5 = ClassBreak(description: "> 86,100 to 10,110,975", label: "> 86,100 to 10,110,975", minValue: 86_101, maxValue: 10_110_975, symbol: symbol5) |
| 110 | + |
| 111 | + return ClassBreaksRenderer(fieldName: "POP2007", classBreaks: [classBreak1, classBreak2, classBreak3, classBreak4, classBreak5]) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +#Preview { |
| 116 | + ApplyClassBreaksRendererToSublayerView() |
| 117 | +} |
0 commit comments