struct MapViewExample: UIViewRepresentable {
let location = CLLocation()
func makeUIView(context: Context) -> MKMapView {
let mapView = MKMapView()
mapView.showsUserLocation = true
mapView.userTrackingMode = .followWithHeading
let camera = MKMapCamera(
lookingAtCenter: location.coordinate,
fromDistance: 100,
pitch: 0,
heading: location.course
)
mapView.setCamera(camera, animated: true)
return mapView
}
func updateUIView(_ uiView: MKMapView, context: Context) {
print(uiView.camera.altitude)
}
}
This example creates a MKMapView that shows the user's location, follows the user's heading, and sets a default camera altitude of 100. The altitude value is then logged to the console every time the view updates.
Console output always starts with the right value, but then it gets overridden:
100.0
100.0
3613.103143079447
3613.103143079447
3613.103143079447
3613.103143079447