美文网首页
MKMapView的一个用于计算缩放比例的扩展

MKMapView的一个用于计算缩放比例的扩展

作者: 树袋熊老公 | 来源:发表于2019-03-21 16:56 被阅读0次
    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)
        }
        
     
    }
    
    
    

    相关文章

      网友评论

          本文标题:MKMapView的一个用于计算缩放比例的扩展

          本文链接:https://www.haomeiwen.com/subject/vqfqvqtx.html