1

I can't seem to find the best answer to my question SO.

I have this code that is "OK" but not idea

func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {

        print(mapView.camera.altitude)
        if mapView.camera.altitude < 800.00 && !modifyingMap
        {
            modifyingMap = true
            mapView.camera.altitude = 800.00
            modifyingMap = false
        }
    }

I would like to limit a user's max and min zoom to my map in my app.

any links to the SO answer are greatly appreciated!

Thanks!

Desmond
  • 5,001
  • 14
  • 56
  • 115

2 Answers2

7

You could use the mapView:regionDidChangeAnimated: delegate method to listen for region change events, and if the region is wider/narrower than your maximum/minimum region, set it back to the max/min region with setRegion:animated: to indicate to your user that they can't zoom out/in that far.

e.g.

func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    let coordinate = CLLocationCoordinate2DMake(mapView.region.center.latitude, mapView.region.center.longitude)
    var span = mapView.region.span
    if span.latitudeDelta < 0.002 { // MIN LEVEL
        span = MKCoordinateSpanMake(0.002, 0.002)
    } else if span.latitudeDelta > 0.003 { // MAX LEVEL
        span = MKCoordinateSpanMake(0.003, 0.003)
    }
    let region = MKCoordinateRegionMake(coordinate, span)
    mapView.setRegion(region, animated:true)
}
Kosuke Ogawa
  • 7,383
  • 3
  • 31
  • 52
4

Try this out:

 mapView.cameraZoomRange = MKMapView.CameraZoomRange(
            minCenterCoordinateDistance: 1000, // Minimum zoom value
            maxCenterCoordinateDistance: 10000) // Max zoom value
ExeRhythm
  • 452
  • 5
  • 13