Rotate a map.
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
- Create a
Map
object with thearcGISStreets
basemap style. - Create a
Viewpoint
object with a center, a scale, and a desired starting rotation. - Create a
MapView
instance with the map and viewpoint. - 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
// 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()
}