import Foundation
import MapKit
extension MKMapView {
//缩放级别
var zoomLevel: Double {
//获取缩放级别
get {
return Double(log2(360 * (Double(self.frame.size.width/256)
/ self.region.span.longitudeDelta)) )
}
//设置缩放级别
set (newZoomLevel){
setCenterCoordinate(coordinate: self.centerCoordinate, zoomLevel: newZoomLevel,
animated: false)
}
}
//根据区域范围、中心设置缩放比例
public func setZoomLevel(geofenceCenter:CLLocationCoordinate2D,range:Double) {
let region = MKCoordinateRegionMakeWithDistance(geofenceCenter, range ,range)
let zoomLevel = Double(log2(360 * Double(self.frame.size.width/256)
/ region.span.longitudeDelta) )
self.zoomLevel = zoomLevel
}
//设置缩放级别时调用
private func setCenterCoordinate(coordinate: CLLocationCoordinate2D, zoomLevel: Double,
animated: Bool){
let span = MKCoordinateSpanMake(0,
360 / pow(2, Double(zoomLevel)) * Double(self.frame.size.width) / 256)
setRegion(MKCoordinateRegionMake(centerCoordinate, span), animated: animated)
}
}
网友评论