1

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

Saranya
  • 163
  • 2
  • 10
  • 1
    Possible duplicate of this? http://stackoverflow.com/questions/1636868/is-there-way-to-limit-mkmapview-maximum-zoom-level – Praveen Kumar Oct 18 '16 at 16:05
  • 1
    @Prav it is explained in objective c but none related to swit. So I am posting this question – Saranya Oct 18 '16 at 16:07
  • 1
    Possible duplicate of [Is there way to limit MKMapView maximum zoom level?](https://stackoverflow.com/questions/1636868/is-there-way-to-limit-mkmapview-maximum-zoom-level) – user3474985 Dec 05 '18 at 15:07

2 Answers2

7

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)
    }
}
Praveen Kumar
  • 1,338
  • 3
  • 20
  • 31
  • Your solution is partially corret – Saranya Oct 18 '16 at 18:13
  • 2
    Your solution is partially correct but there is a problem with this Int type. For example if zoom level is 13.1 to 13.4 is rounded to 13. So very smaller zoom is not working perfectly – Saranya Oct 18 '16 at 18:15
-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.

Eivind
  • 1
  • This only changes the min and max zoom level for displaying TileOverlays and is not related to the actual zooming on the Map itself. – Jordi Bruin Jan 20 '19 at 21:55