Saranya
Saranya

Reputation: 163

Maximum zoom level in iOS map using swift

I don't want the user to zoom out more. Is there a way to set max zoom level in Map kit using swift?

Upvotes: 1

Views: 3355

Answers (2)

Eivind
Eivind

Reputation: 1

How to limit zoomlevel:

class CustomMapOverlay: MKTileOverlay {

.
init() {
  self.minimumZ = 3
  self.maximumZ = 15
}

}

or set min and maximumZ values in override func url(forTilePath path. If other custom map overlays are used with higher maximumZ = 17 level at the same time then this 17 value will override our 15 level value.

Upvotes: -1

Praveen Kumar
Praveen Kumar

Reputation: 1340

Please check if this satisfies your condition.

import Foundation
import MapKit

class MapViewWithZoom: MKMapView {

    var zoomLevel: Int {
        get {
            return Int(log2(360 * (Double(self.frame.size.width/256) / self.region.span.longitudeDelta)) + 1);
        }

        set (newZoomLevel){
            setCenterCoordinate(self.centerCoordinate, zoomLevel: newZoomLevel, animated: false)
        }
    }

    private func setCenterCoordinate(coordinate: CLLocationCoordinate2D, zoomLevel: Int, animated: Bool){
        let span = MKCoordinateSpanMake(0, 360 / pow(2, Double(zoomLevel)) * Double(self.frame.size.width) / 256)
        setRegion(MKCoordinateRegionMake(centerCoordinate, span), animated: animated)
    }
}

Upvotes: 7

Related Questions