如何使用 Apple MapKit 从 VisibleRegion 获取 lat/lng 坐标?

How to get lat/lng coordinates from VisibleRegion using Apple MapKit?

我需要可见 mapView 的 upperRight 和 bottomLeft 坐标。

在 mapViewDidChangeVisibleRegion 中,我得到了值。

class ViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet weak var mapView: MKMapView!

    var currentLocation: CLLocation!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.mapView.delegate = self

        self.customizeMapView()

        currentLocation =  CLLocation(latitude: ( 37.334922), longitude: (-122.009033))

        self.centerMapOnLocation()

   }

    func customizeMapView(){
        self.mapView.showsBuildings = true
        self.mapView.showsCompass = true
        self.mapView.showsPointsOfInterest = true
        self.mapView.showsUserLocation = false
        self.mapView.userTrackingMode = .none
        self.mapView.showsZoomControls = true
        self.mapView.mapType = .satelliteFlyover       
    }

    func centerMapOnLocation(){

        let centerRegionCoordinate: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude)

        let spanRegion:MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: 0.0025, longitudeDelta: 0.0025)
        let mapRegion: MKCoordinateRegion = MKCoordinateRegion(center: centerRegionCoordinate, span: spanRegion)
        mapView.setRegion(mapRegion, animated: true)


    }

    func mapViewDidChangeVisibleRegion(_ mapView: MKMapView) {
        print("mapViewDidChangeVisibleRegion")
       /*
         0,0         +x




         +y

        */
        let upperRight = mapView.convert(CGPoint(x: self.view.frame.size.width, y: 0.0), toCoordinateFrom: self.view)
        let bottomLeft = mapView.convert(CGPoint(x: 0.0, y: self.view.frame.size.height), toCoordinateFrom: self.view)

        print("upperRight: \(upperRight) ")
        print("bottomLeft: \(bottomLeft) ")


    }

我希望坐标是正确的,但我得到的是 -180.0。

这些是我最初 运行 应用程序时获得的值:

upperRight: CLLocationCoordinate2D(latitude: -180.0, longitude: -180.0) 
bottomLeft: CLLocationCoordinate2D(latitude: -180.0, longitude: -180.0) 

用手势水平移动地图:

upperRight: CLLocationCoordinate2D(latitude: -180.0, longitude: -180.0) 
bottomLeft: CLLocationCoordinate2D(latitude: 37.33245941071868, longitude: -122.01073312548439) 

用手势垂直移动地图:

upperRight: CLLocationCoordinate2D(latitude: 37.33687329393082, longitude: -122.00780068382419) 
bottomLeft: CLLocationCoordinate2D(latitude: -180.0, longitude: -180.0) 

用手势沿对角线移动地图:

upperRight: CLLocationCoordinate2D(latitude: 37.336306795130724, longitude: -122.00839348216184) 
bottomLeft: CLLocationCoordinate2D(latitude: 37.33126864523067, longitude: -122.01130820828396) 

如何正确执行mapView.convert?

与其计算 mapView 框架来确定坐标,我更愿意使用 MKCoordinateRegion 来获取 bounding-box 坐标,如下所示:

extension MKCoordinateRegion {

    var boundingBoxCoordinates: [CLLocationCoordinate2D] {
        let halfLatDelta = self.span.latitudeDelta / 2
        let halfLngDelta = self.span.longitudeDelta / 2

        let topLeftCoord = CLLocationCoordinate2D(
            latitude: self.center.latitude + halfLatDelta,
            longitude: self.center.longitude - halfLngDelta
        )
        let bottomRightCoord = CLLocationCoordinate2D(
            latitude: self.center.latitude - halfLatDelta,
            longitude: self.center.longitude + halfLngDelta
        )
        let bottomLeftCoord = CLLocationCoordinate2D(
            latitude: self.center.latitude - halfLatDelta,
            longitude: self.center.longitude - halfLngDelta
        )
        let topRightCoord = CLLocationCoordinate2D(
            latitude: self.center.latitude + halfLatDelta,
            longitude: self.center.longitude + halfLngDelta
        )

        return [topLeft, topRight, bottomRight, bottomLeft]
    }

}

用法: 在 top-left、top-right、bottom-right、bottom-leftMKMapView 上显示注释=15=]

class ViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet var mapView: MKMapView!

    private let annotations: [MKPointAnnotation] = [MKPointAnnotation(), MKPointAnnotation(), MKPointAnnotation(), MKPointAnnotation()]

    override func viewDidLoad() {
        super.viewDidLoad()

        self.mapView.addAnnotations(annotations)

        let currentLocationCoordinate = CLLocationCoordinate2D(latitude: 37.334922, longitude: -122.009033)
        let spanRegion = MKCoordinateSpan(latitudeDelta: 0.0025, longitudeDelta: 0.0025)
        let mapRegion = MKCoordinateRegion(center: currentLocationCoordinate, span: spanRegion)
        mapView.setRegion(mapRegion, animated: true)
    }

    func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
        let bounds = mapView.region.boundingBoxCoordinates
        print("TopLeft: \(bounds[0])\nTopRight: \(bounds[1])\nBottomRight: \(bounds[2])\nBottomLeft: \(bounds[3])")
        for (i, coordinate) in bounds.enumerated() {
            self.annotations[i].coordinate = coordinate
        }
    }

}

结果