使用 userTrackingMode = .followWithHeading 时,如何防止 IOS MapKit 自动缩放?也就是说,我将当前用户位置设置为屏幕中心,并设置“view.userTrackingMode = .followWithHeading”,以便地图指向北方,但是当您手动放大/缩小时,MapView 会自动覆盖并缩小到它似乎更喜欢的水平。
我本来可以放大,然后缩放级别保持这样,同时保持地图以用户位置为中心,并保持自动旋转以保持地图与北方对齐。
我正在使用 SwiftUI,因此确实将位置作为参数传递给 GCMapView(作为使 SwiftUI GCMapView 与最新用户位置保持同步的方法)。所以不确定这是否会导致问题?
我正在使用的 MapKit 回调的一些关键位(已提取一些代码以显示相关行):
struct FlightView: View {
@EnvironmentObject var locationManager : GCLocationManager
@State var centreUserLocation : Bool = false
var body: some View {
GCMapView(
flight: flight,
userLocation: locationManager.userLocation,
centreUserLocation: centreUserLocation,
initalZoomDone: initalZoomDone
)
}
}
struct GCMapView : UIViewRepresentable {
let map = MKMapView()
func makeUIView(context: Context) -> MKMapView {
map.delegate = context.coordinator
map.isRotateEnabled = true
map.userTrackingMode = .followWithHeading
map.showsUserLocation = true
return map
}
func updateUIView(_ view: MKMapView, context: Context) {
if let userLocation = userLocation {
view.centerCoordinate = userLocation
view.userTrackingMode = .followWithHeading // Needed to keep map rotating to align to North
}
}
func makeCoordinator() -> Coordinator {
return Coordinator(self)
}
class Coordinator: NSObject, MKMapViewDelegate {
var parent: GCMapView
init(_ parent: GCMapView) {
self.parent = parent
super.init()
}
}
}