我有一个主控制器,其代码类似于在屏幕的一部分中设置Mapbox,并且它可以工作......但是,记录的代码永远不会触发相机视口的移动或点击。我是不是少了一步?
onCameraChange 在日志中显示一次,而不是在我平移地图世界时显示。
import UIKit
import MapboxMaps
class ViewController: UIViewController, MapInitOptionsProvider, URLSessionDelegate {
private var mapView: MapView!
private var cancellables: Set<AnyCancelable> = []
public func mapInitOptions() -> MapInitOptions {
//TODO... never called???
let cameraOptions = CameraOptions(center: CLLocationCoordinate2D(latitude: NUMBER, longitude: NUMBER), zoom: 1)
//return a custom MapInitOptions
return MapInitOptions(
cameraOptions: cameraOptions,
styleURI: .light)// StyleURI(rawValue: "mapbox://my/favorite/style/goes/here..."))
}
override func viewDidLoad() {
let defaults = UserDefaults.standard
super.viewDidLoad()
self.mapView = MapView(frame: view.bounds)
self.mapView.mapboxMap.mapStyle = MapStyle(uri:StyleURI(rawValue: "mapbox://my-favorite-style")!)
self.mapView.mapboxMap.onCameraChanged.observe { [weak self] cameraChanged in
guard let self else { return }
updateMap()
print("onCameraChange")
}.store(in: &cancellables)
self.mapView.gestures.onMapTap.observe { [unowned self] context in
print("onMapTap")
updateMap()
}.store(in: &cancellables)
...
事实证明它需要 IBOutlet,如下所述:
https://www.youtube.com/watch?app=desktop&v=CVqdylyDSao
所以在视图控制器中:
@IBOutlet var nameOfview: MapView!
和nameOfview可以参考。