MKMapView userTrackingMode 在 SwiftUI 中重置

Rob*_*Rob 3 mapkit ios mkusertrackingmode swiftui

MKMapView在 SwiftUI 中显示userTrackingMode设置为 时遇到了麻烦.follow。我正在展示一张地图:

struct ContentView: View {
    var body: some View {
        MapView()
    }
}
Run Code Online (Sandbox Code Playgroud)

在这方面,MapView我 (a) 设置userTrackingMode和 (b) 确保我拥有使用中的权限。我一直在基于故事板的项目中使用这种模式。无论如何,MapView现在看起来像:

final class MapView: UIViewRepresentable {
    private lazy var locationManager = CLLocationManager()

    func makeUIView(context: Context) -> MKMapView {
        if CLLocationManager.authorizationStatus() == .notDetermined {
            locationManager.requestWhenInUseAuthorization()
        }

        let mapView = MKMapView()
        mapView.showsUserLocation = true
        mapView.userTrackingMode = .follow  // no better is mapView.setUserTrackingMode(.follow, animated: true)
        return mapView
    }

    func updateUIView(_ uiView: UIViewType, context: Context) {
        print(#function, uiView.userTrackingMode)
    }
}
Run Code Online (Sandbox Code Playgroud)

这里的一切看起来都不错,但地图(在模拟器和物理设备上)实际上并未处于跟随用户跟踪模式。

所以,我扩展了上面的内容,添加了一个采用MKMapViewDelegate协议的协调器,这样我就可以观察跟踪模式发生了什么:

final class MapView: UIViewRepresentable {
    private lazy var locationManager = CLLocationManager()

    func makeUIView(context: Context) -> MKMapView {
        if CLLocationManager.authorizationStatus() == .notDetermined {
            locationManager.requestWhenInUseAuthorization()
        }

        let mapView = MKMapView()
        mapView.delegate = context.coordinator
        mapView.showsUserLocation = true
        mapView.userTrackingMode = .follow  // no better is mapView.setUserTrackingMode(.follow, animated: true)
        return mapView
    }

    func updateUIView(_ uiView: UIViewType, context: Context) {
        print(#function, uiView.userTrackingMode)
    }

    func makeCoordinator() -> MapViewCoordinator {
        return MapViewCoordinator(self)
    }
}

class MapViewCoordinator: NSObject {
    var mapViewController: MapView

    var token: NSObjectProtocol?

    init(_ control: MapView) {
        self.mapViewController = control
    }
}

extension MapViewCoordinator: MKMapViewDelegate {
    func mapView(_ mapView: MKMapView, didChange mode: MKUserTrackingMode, animated: Bool) {
        print(#function, mode)
    }
}
Run Code Online (Sandbox Code Playgroud)

这导致:

mapView(_:didChange:animated:) MKUserTrackingMode.follow
updateUIView(_:context:) MKUserTrackingMode.follow
mapView(_:didChange:animated:) MKUserTrackingMode.none

有一些事情正在将 重置userTrackingMode.none

对于咯咯笑声,我尝试重置userTrackingMode,这也好不到哪里去:

func updateUIView(_ uiView: UIViewType, context: Context) {
    print(#function, uiView.userTrackingMode)
    uiView.userTrackingMode = .follow
}
Run Code Online (Sandbox Code Playgroud)

不过,这种笨拙的模式确实有效:

func updateUIView(_ uiView: UIViewType, context: Context) {
    print(#function, uiView.userTrackingMode)
    DispatchQueue.main.async {
        uiView.userTrackingMode = .follow
    }
}
Run Code Online (Sandbox Code Playgroud)

或者任何userTrackingMode在这个初始过程之后重置的东西似乎也有效。

我做错了UIViewRepresentable什么吗?一个错误MKMapView


它并不真正相关,但这是我显示跟踪模式的例行程序:

extension MKUserTrackingMode: CustomStringConvertible {
    public var description: String {
        switch self {
        case .none:              return "MKUserTrackingMode.none"
        case .follow:            return "MKUserTrackingMode.follow"
        case .followWithHeading: return "MKUserTrackingMode.followWithHeading"
        @unknown default:        return "MKUserTrackingMode unknown/default"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Rob*_*Rob 8

令人气愤的是,在花费大量时间调试、准备问题等之后,看起来这种奇怪的行为只有frame在初始化期间不提供 a时才会表现出来:

let mapView = MKMapView()
Run Code Online (Sandbox Code Playgroud)

当我使用以下内容时(即使最终地图不是这个大小),它也能正常工作:

let mapView = MKMapView(frame: UIScreen.main.bounds)
Run Code Online (Sandbox Code Playgroud)

我仍然会发布这个,希望它可以将其他人从这场噩梦中拯救出来。