将视点转换为 MKMapView 坐标
Converting View Points to MKMapView Coordinates
我的objective是将我的view的左上角和右下角的点转换成lat/lon坐标。这些 lat/lon 坐标将用于查询仅存在于视图中的注释位置(并非全部 5000+)。
我找到了这个 Objective-C tip on Whosebug。但我遇到的问题是它正在从 mapView 转换 0,0(-180、-180 的 lat/lon。又名南极)。
所以代替:
topLeft = mapView.convertPoint(CGPointMake(0, 0), toCoordinateFromView: self.mapView)
我想我可以简单地做:
topLeft = view.convertPoint(CGPointMake(0, 0), toCoordinateFromView: self.mapView)
但我收到错误:
Cannot invoke 'convertPoint' with an argument list of type '(CGPoint,
toCoordinateFromView: MKMapView!)'
我想了一天,没有结果,特来求教。任何帮助将不胜感激。
完整函数如下:
func findCornerLocations(){
var topLeft = CLLocationCoordinate2D()
let mapView = MKMapView()
topLeft = view.convertPoint(CGPointMake(0, 0), toCoordinateFromView: self.mapView)
print(topLeft.latitude, topLeft.longitude)
}
普通视图没有 convertPoint(_:toCoordinateFromView:)
函数,只有 MKMapView
,它解释了您看到的编译器错误。是什么让您停止使用此版本?
topLeft = mapView.convertPoint(CGPointMake(0, 0), toCoordinateFromView: self.mapView)
此外,如果所有注释都已添加到地图视图中,使用 annotationsInMapRect
方法会更成功:
let visibleAnnotations = mapView.annotationsInMapRect(mapView.visibleMapRect)
for element in visibleAnnotations {
guard let annotation = element as? MKAnnotation
else { continue }
print(annotation)
}
你们非常非常亲密!
let topLeft = map.convertPoint(CGPointMake(0, 0), toCoordinateFromView: self.view)
let bottomleft = map.convertPoint(CGPointMake(0, self.view.frame.size.height), toCoordinateFromView: self.view)
实施后,它将如下所示:
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)")
我的objective是将我的view的左上角和右下角的点转换成lat/lon坐标。这些 lat/lon 坐标将用于查询仅存在于视图中的注释位置(并非全部 5000+)。
我找到了这个 Objective-C tip on Whosebug。但我遇到的问题是它正在从 mapView 转换 0,0(-180、-180 的 lat/lon。又名南极)。
所以代替:
topLeft = mapView.convertPoint(CGPointMake(0, 0), toCoordinateFromView: self.mapView)
我想我可以简单地做:
topLeft = view.convertPoint(CGPointMake(0, 0), toCoordinateFromView: self.mapView)
但我收到错误:
Cannot invoke 'convertPoint' with an argument list of type '(CGPoint, toCoordinateFromView: MKMapView!)'
我想了一天,没有结果,特来求教。任何帮助将不胜感激。
完整函数如下:
func findCornerLocations(){
var topLeft = CLLocationCoordinate2D()
let mapView = MKMapView()
topLeft = view.convertPoint(CGPointMake(0, 0), toCoordinateFromView: self.mapView)
print(topLeft.latitude, topLeft.longitude)
}
普通视图没有 convertPoint(_:toCoordinateFromView:)
函数,只有 MKMapView
,它解释了您看到的编译器错误。是什么让您停止使用此版本?
topLeft = mapView.convertPoint(CGPointMake(0, 0), toCoordinateFromView: self.mapView)
此外,如果所有注释都已添加到地图视图中,使用 annotationsInMapRect
方法会更成功:
let visibleAnnotations = mapView.annotationsInMapRect(mapView.visibleMapRect)
for element in visibleAnnotations {
guard let annotation = element as? MKAnnotation
else { continue }
print(annotation)
}
你们非常非常亲密!
let topLeft = map.convertPoint(CGPointMake(0, 0), toCoordinateFromView: self.view)
let bottomleft = map.convertPoint(CGPointMake(0, self.view.frame.size.height), toCoordinateFromView: self.view)
实施后,它将如下所示:
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)")