在我的应用程序中,要求用户不能缩放地图直到街道水平。我尝试了以下代码,但无法正常工作。通过此代码在某个缩放点,用户将无法放大或缩小。
func mapView(_ mapView: MKMapView,regionDidChangeAnimated animated: Bool){
if self.mapView.currentZoomLevel > 16 {
self.mapView.setCenterCoordinate(self.mapView.centerCoordinate, withZoomLevel: 16, animated: false)
}
}
extension MKMapView {
open var currentZoomLevel: Int {
let maxZoom: Double = 24
let zoomScale = visibleMapRect.size.width / Double(frame.size.width)
let zoomExponent = log2(zoomScale)
return Int(maxZoom - ceil(zoomExponent))
}
open func setCenterCoordinate(_ centerCoordinate: CLLocationCoordinate2D,
withZoomLevel zoomLevel: Int,
animated: Bool) {
let minZoomLevel = min(zoomLevel, 28)
let span = coordinateSpan(centerCoordinate, andZoomLevel: minZoomLevel)
let region = MKCoordinateRegion(center: centerCoordinate, span: span)
setRegion(region, animated: animated)
}
}