Desmond
Desmond

Reputation: 5001

Swift Mapkit max min zoom

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!

Upvotes: 1

Views: 4657

Answers (2)

ExeRhythm
ExeRhythm

Reputation: 480

Try this out:

 mapView.cameraZoomRange = MKMapView.CameraZoomRange(
            minCenterCoordinateDistance: 1000, // Minimum zoom value
            maxCenterCoordinateDistance: 10000) // Max zoom value

Upvotes: 5

Kosuke Ogawa
Kosuke Ogawa

Reputation: 7451

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

Upvotes: 7

Related Questions