How to get radius from visible area of MKmapview?

后端 未结 2 655
盖世英雄少女心
盖世英雄少女心 2021-01-01 02:22

I am able to get visible rectangle of map view and also centre point of map view and span deltas are also get from mkmaap view methods: To get visible are :mapView.vis

2条回答
  •  天涯浪人
    2021-01-01 02:54

    With Swift 3.0 you can use extension to simplify your life:

    extension MKMapView {
    
        func topCenterCoordinate() -> CLLocationCoordinate2D {
            return self.convert(CGPoint(x: self.frame.size.width / 2.0, y: 0), toCoordinateFrom: self)
        }
    
        func currentRadius() -> Double {
            let centerLocation = CLLocation(coordinate: self.centerCoordinate)
            let topCenterCoordinate = self.topCenterCoordinate()
            let topCenterLocation = CLLocation(coordinate: topCenterCoordinate)
            return centerLocation.distance(from: topCenterLocation)
        }
    
    }
    

    With Swift 4.0 :

    extension MKMapView {
    
        func topCenterCoordinate() -> CLLocationCoordinate2D {
            return self.convert(CGPoint(x: self.frame.size.width / 2.0, y: 0), toCoordinateFrom: self)
        }
    
        func currentRadius() -> Double {
            let centerLocation = CLLocation(latitude: self.centerCoordinate.latitude, longitude: self.centerCoordinate.longitude)
            let topCenterCoordinate = self.topCenterCoordinate()
            let topCenterLocation = CLLocation(latitude: topCenterCoordinate.latitude, longitude: topCenterCoordinate.longitude)
            return centerLocation.distance(from: topCenterLocation)
        }
    
    }
    

提交回复
热议问题