Show scale bar

View on GitHub

Add a scale bar to visually gauge distances on a map.

Image of show scale bar

Use case

Allows a user to have a visual reference for distances when navigating the map.

How to use the sample

Zoom in or out of the map. The scale bar will automatically display the appropriate scale based on zoom level. Units can be in metric and/or imperial.

How it works

  1. Instantiate a Map object and display it in a MapView object.
  2. Instantiate a ScaleBar object passing in the map view.
  3. Add the scale bar to the UI overlaying the map view.

Relevant API

  • Map
  • MapView
  • Scalebar
  • UnitSystem

Additional information

The scale will be accurate for the center of the map, and in general more accurate at larger scales (zoomed in). This means at smaller scales (zoomed out), the reading may be inaccurate at the extremes of the visible extent.

Tags

map, measure, scale, toolkit

Sample Code

ShowScaleBarView.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
// 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 ArcGISToolkit
import SwiftUI

struct ShowScaleBarView: View {
    /// The height of the map view's attribution bar.
    @State private var attributionBarHeight = 0.0

    /// Allows for communication between the `Scalebar` and `MapView`.
    @State private var spatialReference: SpatialReference?

    /// Allows for communication between the `Scalebar` and `MapView`.
    @State private var unitsPerPoint: Double?

    /// The maximum screen width allotted to the scalebar.
    private let maxWidth = 175.0

    /// Allows for communication between the `Scalebar` and `MapView`.
    @State private var viewpoint: Viewpoint?

    /// The location of the scalebar on screen.
    private let alignment: Alignment = .bottomLeading

    /// A map with a topographic style.
    @State private var map: Map = {
        let map = Map(basemapStyle: .arcGISTopographic)
        // Creates an initial viewpoint with a coordinate point
        // centered on San Franscisco's Golden Gate Bridge.
        map.initialViewpoint = Viewpoint(
            center: Point(x: -13637000, y: 4550000, spatialReference: .webMercator),
            scale: 100_000
        )
        return map
    }()

    // The `ScalebarSettings` add the shadow to the scale bar.
    @State private var scaleBarSettings: ScalebarSettings = {
        let settings = ScalebarSettings(
            shadowColor: Color.black,
            shadowRadius: 6
        )
        return settings
    }()

    var body: some View {
        MapView(map: map)
            .onAttributionBarHeightChanged { newHeight in
                withAnimation { attributionBarHeight = newHeight }
            }
            .onSpatialReferenceChanged { spatialReference = $0 }
            .onUnitsPerPointChanged { unitsPerPoint = $0 }
            .onViewpointChanged(kind: .centerAndScale) { viewpoint = $0 }
            .overlay(alignment: alignment) {
                Scalebar(
                    maxWidth: maxWidth,
                    settings: scaleBarSettings,
                    spatialReference: spatialReference,
                    style: .graduatedLine,
                    unitsPerPoint: unitsPerPoint,
                    viewpoint: viewpoint
                )
                // The styling around scale bar.
                .padding([.leading, .trailing], 20)
                .padding([.horizontal, .vertical], 10)
                .background(Color.white)
                .opacity(0.8)
                .cornerRadius(8)
                .padding(.vertical, 10 + attributionBarHeight)
                .padding(10)
            }
    }
}

#Preview {
    ShowScaleBarView()
}

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