Apply a unique value with alternate symbols at different scales.
Use case
When a layer is symbolized with unique value symbology, you can specify the visible scale range for each unique value. This is an effective strategy to limit the amount of detailed data at smaller scales without having to make multiple versions of the layer, each with a unique definition query.
Once scale ranges are applied to unique values, you can further refine the appearance of features within those scale ranges by establishing alternate symbols to different parts of the symbol class scale range.
How to use the sample
Zoom in and out of the map to see alternate symbols at each scale. The symbology changes according to the following scale ranges: 0-5,000, 5,000-10,000, 10,000-20,000. To go back to the initial viewpoint, tap "Reset Viewpoint".
How it works
- Create a
FeatureLayer
using the service URL and add it to the map's list of operational layers. - Create two alternate symbols (a blue square and a yellow diamond) to be used as alternate symbols. To create an alternate symbol:
- Create a symbol using
SimpleMarkerSymbol
. - Convert the simple marker symbol to an
MultilayerSymbol
usingSimpleMarkerSymbol.toMultilayerSymbol()
. - Set the multilayer symbol's
referenceProperties
to the valid scale ranges with the blue square and yellow diamond.
- Create a symbol using
- Create a third multilayer symbol to be used to create a
UniqueValue
class. - Create a unique value using the red triangle from step 3 and the list of alternate symbols from step 2.
- Create a
UniqueValueRenderer
and add the unique value from step 4 to it. - Create a purple diamond simple marker and convert it to a multilayer symbol to be used as the default symbol.
- Set the unique value renderer's
defaultSymbol
property to the purple diamond from step 6. - Set the unique value renderer's
fieldNames
property to "req_type". - Apply this unique value renderer to the renderer on feature layer.
Relevant API
- MultilayerSymbol
- SimpleMarkerSymbol
- SymbolReferenceProperties
- UniqueValue
- UniqueValueRenderer
About the data
The San Francisco 311 incidents layer in this sample displays point features related to crime incidents such as graffiti and tree damage that have been reported by city residents.
Tags
alternate symbols, multilayer symbol, scale based rendering, simple marker symbol, symbol reference properties, symbology, unique value, unique value renderer
Sample Code
// Copyright 2024 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
//
// https://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 ArcGIS
import SwiftUI
struct DisplayAlternateSymbolsAtDifferentScalesView: View {
/// A map with a layer of the incidents in San Francisco, CA.
@State private var map: Map = {
let map = Map(basemapStyle: .arcGISTopographic)
// Creates the feature layer from a URL and adds it to the map.
let featureTable = ServiceFeatureTable(url: .sf311IncidentsLayer)
let featureLayer = FeatureLayer(featureTable: featureTable)
map.addOperationalLayer(featureLayer)
// Sets the render on the feature layer.
featureLayer.renderer = makeUniqueValueRenderer()
let center = Point(x: -13631200, y: 4546830, spatialReference: .webMercator)
map.initialViewpoint = Viewpoint(center: center, scale: 7_500)
return map
}()
/// The current viewpoint of the map view.
@State private var viewpoint: Viewpoint?
var body: some View {
MapView(map: map, viewpoint: viewpoint)
.onViewpointChanged(kind: .centerAndScale) { newViewpoint in
viewpoint = newViewpoint
}
.overlay(alignment: .top) {
Text("Scale: 1:\(viewpoint?.targetScale ?? 0, format: .number.rounded(increment: 1))")
.multilineTextAlignment(.center)
.frame(maxWidth: .infinity, alignment: .center)
.padding(8)
.background(.regularMaterial, ignoresSafeAreaEdges: .horizontal)
}
.toolbar {
ToolbarItem(placement: .bottomBar) {
Button("Reset Viewpoint") {
viewpoint = map.initialViewpoint
}
}
}
}
/// Creates a unique value renderer with alternate symbols for different scales.
/// - Returns: A new `UniqueValueRenderer` object.
private static func makeUniqueValueRenderer() -> UniqueValueRenderer {
// The multilayer symbol for the low range scale.
let redTriangle = SimpleMarkerSymbol(style: .triangle, color: .red, size: 30)
.toMultilayerSymbol()
redTriangle.referenceProperties = SymbolReferenceProperties(minScale: 5_000, maxScale: 0)
// The alternate multilayer symbol for the mid range scale.
let blueSquare = SimpleMarkerSymbol(style: .square, color: .blue, size: 30)
.toMultilayerSymbol()
blueSquare.referenceProperties = SymbolReferenceProperties(minScale: 10_000, maxScale: 5_000)
// The alternate multilayer symbol for the high range scale.
let yellowDiamond = SimpleMarkerSymbol(style: .diamond, color: .yellow, size: 30)
.toMultilayerSymbol()
yellowDiamond.referenceProperties = SymbolReferenceProperties(minScale: 20_000, maxScale: 10_000)
let uniqueValue = UniqueValue(
description: "unique values based on request type",
label: "unique value",
symbol: redTriangle,
values: ["Damaged Property"],
alternateSymbols: [blueSquare, yellowDiamond]
)
// The default symbol for values that don’t match the unique values.
let purpleDiamond = SimpleMarkerSymbol(style: .diamond, color: .purple, size: 15)
.toMultilayerSymbol()
return UniqueValueRenderer(
fieldNames: ["req_type"],
uniqueValues: [uniqueValue],
defaultSymbol: purpleDiamond
)
}
}
private extension URL {
/// The web URL to the SF311 feature service "Incidents" layer on ArcGIS Online.
static var sf311IncidentsLayer: URL {
URL(string: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/SF311/FeatureServer/0")!
}
}
#Preview {
DisplayAlternateSymbolsAtDifferentScalesView()
}