4
votes

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)
}
2
Just to clarify, you're trying to get the coordinates of a point in your View that contains your MapView, right?MQLN
@MacLean Correct. Points 0,0 (topLeft) and view.frame.size.width/height (bottomRight).Geppelt
@MacLean but so far I have been just trying to get the topLeft coordinates to print out. Will implement bottomRight when I figure it out.Geppelt
Michael, I just added all the code I used for my test. Let me know if it doesn't work, even when implemented my way. BestMQLN
Please remove "Solved" from your title and post your own solution as a proper answer.Jongware

2 Answers

8
votes

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)")
0
votes

A normal view doesn't have the convertPoint(_:toCoordinateFromView:) function, only an MKMapView, which explains the compiler error you're seeing. What made you stop using this version?

topLeft = mapView.convertPoint(CGPointMake(0, 0), toCoordinateFromView: self.mapView)

Additionally, if all the annotations are already added to the map view, you'll have much better success using the annotationsInMapRect method:

let visibleAnnotations = mapView.annotationsInMapRect(mapView.visibleMapRect)
for element in visibleAnnotations {
    guard let annotation = element as? MKAnnotation
        else { continue }

    print(annotation)
}