将视点转换为MKMapView坐标

Gep*_*elt 3 mapkit mkmapview ios swift

我的目标是将视图的左上角和右下角转换为纬度/经度坐标.这些lat/lon坐标将用于查询仅存在于视图中的注释位置(不是全部5000+).

我在Stackoverflow上找到了这个Objective-C技巧.但我遇到的问题是它正在从mapView转换0,0(纬度/经度为-180,-180.Aka,南极).

所以代替:

topLeft = mapView.convertPoint(CGPointMake(0, 0), toCoordinateFromView: self.mapView)
Run Code Online (Sandbox Code Playgroud)

我想我可以做到:

topLeft = view.convertPoint(CGPointMake(0, 0), toCoordinateFromView: self.mapView)
Run Code Online (Sandbox Code Playgroud)

但我得到错误:

无法使用类型'(CGPoint,toCoordinateFromView:MKMapView!)的参数列表调用'convertPoint'

我花了一天时间试图解决这个问题,但无济于事,我来找你寻求指导.任何帮助将非常感激.

这是完整的功能:

func findCornerLocations(){

    var topLeft = CLLocationCoordinate2D()
    let mapView = MKMapView()
    topLeft = view.convertPoint(CGPointMake(0, 0), toCoordinateFromView: self.mapView)

    print(topLeft.latitude, topLeft.longitude)
}
Run Code Online (Sandbox Code Playgroud)

MQL*_*QLN 7

你非常,非常接近!

let topLeft = map.convertPoint(CGPointMake(0, 0), toCoordinateFromView: self.view)
let bottomleft = map.convertPoint(CGPointMake(0, self.view.frame.size.height), toCoordinateFromView: self.view)
Run Code Online (Sandbox Code Playgroud)

实施时,它看起来像这样:

        let map = MKMapView()
        map.frame = CGRectMake(100, 100, 100, 100)
        let coord = CLLocationCoordinate2DMake(37, -122)
        let span = MKCoordinateSpanMake(1, 1)
        map.region = MKCoordinateRegionMake(coord, span)
        self.view.addSubview(map)

        let topleft = map.convertPoint(CGPointMake(0, 0), toCoordinateFromView: self.view)
        let bottomleft = map.convertPoint(CGPointMake(0, self.view.frame.size.height), toCoordinateFromView: self.view)

        print("top left = \(topleft)")
        print("bottom left = \(bottomleft)")
Run Code Online (Sandbox Code Playgroud)