将视点转换为MKMapView坐标 [英] Converting View Points to MKMapView Coordinates
问题描述
我的目标是将视图的左上角和右下角转换为纬度/经度坐标。这些lat / lon坐标将用于查询仅存在于视图中的注释位置(不是全部5000 +)。
My objective is to convert the top left and bottom right points of my view to lat/lon coordinates. These lat/lon coordinates will be used to query annotation locations that only exist within the view (not all 5000+).
我发现这个目标 - Stackoverflow上的C提示。但我遇到的问题是它从mapView转换0,0(纬度/经度为-180,-180.Aka,南极)。
I found this Objective-C tip on Stackoverflow. But the issue I have is that it is converting 0,0 from the mapView (a lat/lon of -180,-180. Aka, the Southpole).
所以而不是:
topLeft = mapView.convertPoint(CGPointMake(0, 0), toCoordinateFromView: self.mapView)
我想我可以这样做:
topLeft = view.convertPoint(CGPointMake(0, 0), toCoordinateFromView: self.mapView)
但我收到错误:
无法使用类型'的参数列表调用'convertPoint'(CGPoint,
toCoordinateFromView:MKMapView!)'
Cannot invoke 'convertPoint' with an argument list of type '(CGPoint, toCoordinateFromView: MKMapView!)'
我花了一天的时间试图弄明白,但无济于事,我来了给你寻求指导。任何帮助将不胜感激。
I have spent a day trying to figure it out, but to no avail, and I have come to you seeking guidance. Any help would be much appreciated.
这是完整的功能:
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)
实现时,它看起来像这样:
When implemented, it'll look like this:
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)")
这篇关于将视点转换为MKMapView坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!