Style geometry types with symbols

View on GitHub

Use a symbol to display a geometry on a map.

Screenshot of Style geometry types with symbols sample

Use case

Customize the appearance of a geometry type with a symbol style suitable for the data. For example, a tourism office may use pictures of landmarks as symbols on an online map or app to help prospective visitors orient themselves more easily around a city. A point on the map styled with a circle could represent a drilled borehole location, whereas a cross could represent the location of an old coal mine shaft. A red line with a dashed style could represent a geological fault mapped on a geological map. A polygon with a brown 'forward-diagonal' fill style could represent an area of artificial ground mapped on a geological map.

How to use the sample

Tap "Edit Styles" and select a geometry to edit with the picker. Use the controls to change the symbol properties for the geometry.

How it works

  1. Create a PictureMarkerSymbol or SimpleMarkerSymbol to style a Point.
    • For the picture marker symbol, create it using a URL or image and set its height property.
    • For the simple marker symbol, set the style, color, and size properties.
  2. Create a SimpleLineSymbol to style a Polyline.
    • Set the style, color, and size properties.
  3. Create a SimpleFillSymbol to style a Polygon.
    • Set the style, color, and outline properties.
  4. Create Graphics using the geometries and symbols and add them to a GraphicsOverlay.
  5. Add the graphics overlay to a MapView.

Relevant API

  • Geometry
  • Graphic
  • GraphicsOverlay
  • PictureMarkerSymbol
  • SimpleFillSymbol
  • SimpleLineSymbol
  • SimpleMarkerSymbol

Tags

display, fill, graphics, line, marker, overlay, picture, point, symbol, visualization

Sample Code

StyleGeometryTypesWithSymbolsView.swiftStyleGeometryTypesWithSymbolsView.swiftStyleGeometryTypesWithSymbolsView.Views.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// 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 StyleGeometryTypesWithSymbolsView: View {
    /// The view model for the sample.
    @StateObject private var model = Model()

    /// A Boolean value indicating whether the edit styles view is presented.
    @State private var isEditStyles = false

    var body: some View {
        MapView(map: model.map, graphicsOverlays: [model.graphicsOverlay])
            .toolbar {
                ToolbarItem(placement: .bottomBar) {
                    editStylesButton
                }
            }
    }

    /// The button for presenting the edit styles view.
    private var editStylesButton: some View {
        Button("Edit Styles") {
            isEditStyles = true
        }
        .popover(isPresented: $isEditStyles) {
            editStyles
                .presentationDetents([.fraction(0.5)])
                .frame(idealWidth: 320, idealHeight: 380)
        }
    }

    /// The view for editing the styles of the geometries.
    private var editStyles: some View {
        NavigationStack {
            SymbolsEditor(model: model)
                .navigationTitle("Edit Styles")
                .navigationBarTitleDisplayMode(.inline)
                .toolbar {
                    ToolbarItem(placement: .confirmationAction) {
                        Button("Done") {
                            isEditStyles = false
                        }
                    }
                }
        }
    }
}

extension StyleGeometryTypesWithSymbolsView {
    /// The view model for the sample.
    final class Model: ObservableObject {
        /// A map with topographic basemap initially centered on Woolgarston, England.
        let map: Map = {
            let map = Map(basemapStyle: .arcGISTopographic)
            map.initialViewpoint = Viewpoint(center: Point(x: -225e3, y: 6_553e3), scale: 88e3)
            return map
        }()

        /// A graphics overlay for displaying the geometry graphics on the map view.
        let graphicsOverlay = GraphicsOverlay()

        /// The simple marker symbol for styling the point.
        let pointSymbol = SimpleMarkerSymbol(style: .circle, color: .purple, size: 12)

        /// The simple line symbol for styling the polyline.
        let polylineSymbol = SimpleLineSymbol(style: .dashDotDot, color: .red, width: 6)

        /// The simple fill symbol for styling the polygon.
        let polygonSymbol = SimpleFillSymbol(
            style: .forwardDiagonal,
            color: .blue,
            outline: SimpleLineSymbol(style: .solid, color: .green, width: 3)
        )

        init() {
            // Creates and adds graphics to the graphics overlay.
            let graphics = makeGraphics()
            graphicsOverlay.addGraphics(graphics)
        }

        /// Creates the graphics for the sample.
        /// - Returns: Graphics with a geometry and symbol.
        private func makeGraphics() -> [Graphic] {
            // Creates graphics using a geometry and symbol.
            let point = Point(x: -225e3, y: 6_560e3)
            let pointGraphic = Graphic(geometry: point, symbol: pointSymbol)

            let polyline = Polyline(points: [
                Point(x: -223e3, y: 6_559e3),
                Point(x: -227e3, y: 6_559e3)
            ])
            let polylineGraphic = Graphic(geometry: polyline, symbol: polylineSymbol)

            let polygon = Polygon(points: [
                Point(x: -222e3, y: 6_558e3),
                Point(x: -228e3, y: 6_558e3),
                Point(x: -228e3, y: 6_555e3),
                Point(x: -222e3, y: 6_555e3)
            ])
            let polygonGraphic = Graphic(geometry: polygon, symbol: polygonSymbol)

            // Creates graphics using points and picture marker symbols.
            let pinSymbol = makePictureMarkerSymbolFromImage()
            let pinPoint = Point(x: -226_770, y: 6_550_470)
            let pinGraphic = Graphic(geometry: pinPoint, symbol: pinSymbol)

            let campsiteSymbol = makePictureMarkerSymbolFromURL()
            let campsitePoint = Point(x: -223_560, y: 6_552_020)
            let campsiteGraphic = Graphic(geometry: campsitePoint, symbol: campsiteSymbol)

            return [pointGraphic, polylineGraphic, polygonGraphic, pinGraphic, campsiteGraphic]
        }

        /// Creates a picture marker symbol from an image in the project assets.
        /// - Returns: A new `PictureMarkerSymbol` object.
        private func makePictureMarkerSymbolFromImage() -> PictureMarkerSymbol {
            let pinSymbol = PictureMarkerSymbol(image: .pinBlueStar)

            // Changes the symbol's offset, so the symbol aligns properly to the point.
            pinSymbol.offsetY = pinSymbol.image!.size.height / 2

            return pinSymbol
        }

        /// Creates a picture marker symbol using a remote image.
        /// - Returns: A new `PictureMarkerSymbol` object.
        private func makePictureMarkerSymbolFromURL() -> PictureMarkerSymbol {
            let imageURL = URL(
                string: "https://static.arcgis.com/images/Symbols/OutdoorRecreation/Camping.png"
            )!
            let campsiteSymbol = PictureMarkerSymbol(url: imageURL)

            campsiteSymbol.width = 25
            campsiteSymbol.height = 25

            return campsiteSymbol
        }
    }
}

#Preview {
    StyleGeometryTypesWithSymbolsView()
}

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