Search code examples
iosswiftmkmapviewmapkitmkmapviewdelegate

how can I fetch the longitude and latitude from a specific point on my mapview in swift?


I have a MKMapView in my app and currently I'm fetching the center point of this map by using:

longitude = mapView.centerCoordinate.longitude
latitude = mapView.centerCoordinate.latitude

but now instead of taking the center point of the map I want to take a specific point that is 100px below the top edge of my mapview. So normally I would use something like:

longitude = mapView.centerCoordinate.longitude
latitude = mapView.(centerCoordinate+100px).latitude

But that's not how that works apparently. Is there any way of fetching a specific point from mkmapview?


Solution

  • Try this:

    let frame = mapView.frame
    let myPoint = CGPointMake(frame.midX, 100)
    let myCoordinate = mapView.convertPoint(myPoint, toCoordinateFromView: mapView)
    
    let annotation = MKPointAnnotation()
    annotation.coordinate = myCoordinate
    annotation.title = "My Point"
    
    mapView.addAnnotation(annotation)