user10813210
user10813210

Reputation:

How can I detect a change in zoom level in mapbox map?

I am trying to change certain properties of a map and of things on a map when as the user zooms in or out certain distances. How can this be achieved? I have tried:

func mapView(_ mapView: MGLMapView, didChange mode: MGLUserTrackingMode, animated: Bool) {
    print(mode, "This is the mode")
    print(mapView.centerCoordinate, "This is the map view ")
}

But That did not seem to print anything inside the method when I moved around.

Upvotes: 2

Views: 1787

Answers (2)

Nico Cobelo
Nico Cobelo

Reputation: 767

Swift 6

Using mapView.gestures.pinchGestureRecognizer worked for me

  override func viewDidLoad() {
        super.viewDidLoad()
        mapView.gestures.pinchGestureRecognizer.addTarget(self, action: #selector(handleZoom))
    }

    @objc private func handleZoom(_: UIBarButtonItem) {
    // Do whatever you need to do when the zoom value changes
        print(mapView.cameraState.zoom)
    }

Upvotes: 0

Shamas S
Shamas S

Reputation: 7549

You can use the delegate method for this. mapView:regionDidChangeAnimated:

A complete list is mentioned here.

func mapViewRegionIsChanging(_ mapView: MGLMapView) {
    print(mapView.zoomLevel, " Cenetr -<<<<")
}

Upvotes: 3

Related Questions