Set viewpoint rotation

View on GitHub

Rotate a map.

Screenshot of set viewpoint rotation sample

Use case

A user may wish to view the map in an orientation other than north-facing.

How to use the sample

Use the slider or pinch to rotate the map. If the map is not pointed north, the compass will display the current heading. Tap the compass to set the map's heading to north.

How it works

  1. Create a Map object with the arcGISStreets basemap style.
  2. Create a Viewpoint object with a center, a scale, and a desired starting rotation.
  3. Create a MapView instance with the map and viewpoint.
  4. Update the viewpoint with a different rotation to change the rotation angle.

Relevant API

  • ArcGIS.MapView
  • ArcGISToolkit.Compass

Tags

rotate, rotation, toolkit, viewpoint

Sample Code

SetViewpointRotationView.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
// Copyright 2022 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 SetViewpointRotationView: View {
    /// The center of the viewpoint.
    @State private var center = Point(x: -117.156229, y: 32.713652, spatialReference: .wgs84)

    /// The scale of the viewpoint.
    @State private var scale = 5e4

    /// The rotation angle for the viewpoint.
    @State private var rotation = Double.zero

    /// A map with ArcGIS Streets basemap style.
    @State private var map = Map(basemapStyle: .arcGISStreets)

    var body: some View {
        VStack {
            MapViewReader { mapViewProxy in
                MapView(map: map, viewpoint: Viewpoint(center: center, scale: scale, rotation: rotation))
                    .onViewpointChanged(kind: .centerAndScale) { viewpoint in
                        center = viewpoint.targetGeometry.extent.center
                        scale = viewpoint.targetScale
                        rotation = viewpoint.rotation
                    }
                    .overlay(alignment: .topTrailing) {
                        Compass(rotation: rotation, mapViewProxy: mapViewProxy)
                            .autoHideDisabled()
                            .padding()
                    }
            }

            HStack {
                // Create a slider to rotate the map.
                Slider(value: $rotation, in: 0...359)

                Text(
                    Measurement(
                        value: rotation,
                        unit: UnitAngle.degrees
                    ),
                    format: .measurement(
                        width: .narrow,
                        numberFormatStyle: .number.precision(.fractionLength(0))
                    )
                )
                .frame(minWidth: 60, alignment: .leading)
            }
            .padding(.horizontal, 50)
            .frame(maxWidth: 540)
        }
        .padding(.bottom)
    }
}

#Preview {
    SetViewpointRotationView()
}

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