How to get left-top and right-bottom latitude and longitude of map in MapKit

前端 未结 2 1668
走了就别回头了
走了就别回头了 2020-12-09 20:17

How to get left-top and right-bottom latitude and longitude of map in MapKit? I use this code, but it doesn\'t work properly. How should I fix it?

MKCoordina         


        
2条回答
  •  孤城傲影
    2020-12-09 20:56

    I created an extension for MKMapView

    Swift 4.2

    extension MKMapView {
        func topLeftCoordinate() -> CLLocationCoordinate2D {
            return convert(.zero, toCoordinateFrom: self)
        }
    
        func bottomRightCoordinate() -> CLLocationCoordinate2D {
            return convert(CGPoint(x: frame.width, y: frame.height), toCoordinateFrom: self)
        }
    }
    

    Swift 2.0

    extension MKMapView {
        func topLeftCoordinate() -> CLLocationCoordinate2D {
            return convertPoint(CGPoint.zero, toCoordinateFromView: self)
        }
    
        func bottomRightCoordinate() -> CLLocationCoordinate2D {
            return convertPoint(CGPoint(x: frame.width, y: frame.height), toCoordinateFromView: self)
        }
    }
    

提交回复
热议问题