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
- SwiftUI Map position being updated by incoming GPS co-ordinates from CLLocationManager
- A user can zoom in/out themselves on the map
- On subsequent GPS position changes I want to move the map to the new position only, and NOT change the zoom in/out (i.e. "distance" when using MapCamera) e
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
:Then you can use
distance
when creating the newMapCamera
.