我的目标是将视图的左上角和右下角转换为纬度/经度坐标.这些lat / lon坐标将用于查询仅存在于视图中的注释位置(不是全部5000). 我发现这个Objective-C tip on Stackoverflow.但我遇到的问题是它
我发现这个Objective-C tip on Stackoverflow.但我遇到的问题是它从mapView转换0,0(纬度/经度为-180,-180.Aka,南极).
所以代替:
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)
}
你非常,非常接近!
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)")
