How would one update the position of a SwiftUI Map without impacting the zoom (or distance from a MapCamera point of view). So want overall want
Issue: In my code below this is not working as when trying to get the "current distance" (aka Zoom) from MapCamera, which is "mapCamPost.camera?distance", this value seems to go to "nil" after the User zooms in the map. How can I fix this? Or another method with the new SwiftUI Map to solve this? (note I didn't have this issue with UIKit MKMapCamera)
struct GCMap: View {
@StateObject var locationMgr = GcFlightState()
@State private var mapCamPos: MapCameraPosition = .automatic
var body: some View {
ZStack {
Map(position: $mapCamPos) {
Annotation("UserLocation", coordinate: self.locationMgr.location.coordinate) {
Image(systemName: "airplane.circle").rotationEffect(.degrees(270))
}
}
.onMapCameraChange() {
print("onMapCameraChange \(mapCamPos.camera?.distance)")
}
.onReceive(locationMgr.$location) { location in
mapCamPos = .camera(MapCamera(
centerCoordinate: location.coordinate,
distance: mapCamPos.camera?.distance ?? 1000, // <<===
heading: location.course
))
}
}
}
}
You can track the camera's distance
using an extra @State
:
@State private var distance = 1000.0
.onMapCameraChange(frequency: .continuous) {
distance = $0.camera.distance
}
Then you can use distance
when creating the new MapCamera
.