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+).
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).
So instead of:
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)
}
You were very, very close!
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)")