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)
I figured I could simply do:
topLeft = view.convertPoint(CGPointMake(0, 0), toCoordinateFromView: self.mapView)
But I get the error:
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.
Here is the complete function:
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)")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With