iosswiftmapboxmapbox-ios

Why doesn't Swift trigger onCameraChanged, Mapbox?


I have a main controller with code like this setting up a Mapbox in a portion of the screen, and it works... However the code as documented does not ever trigger the camera viewport moved or tapped. Am I missing a step?

The onCameraChange shows in the log once and not when I pan around the map world.

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)
  ...
        

Solution

  • It turns out it needed IBOutlet as described here:

    https://www.youtube.com/watch?app=desktop&v=CVqdylyDSao

    So within viewcontroller:

    @IBOutlet var nameOfview: MapView!
    

    and nameOfview can be referenced.