Search code examples
swiftmkmaprect

Converting MKMapRect.origin to longitude and latitude


I have a need to annotate a visible point on a map(without user input). I am using MKMapView visibleMapRect to calculate the point I want. However from my codes below, I am reading values like

let rect = mapView.visibleMapRect
print(rect.origin.x) //211653840.83766
print(rect.origin.y) //133214809.161136

which are not longitude and latitude. But any example I found of conversion between MKMapRect and Coordinates does it like as though the origin is already given in longitude and latitude. Is there something I need to do to get longitude and latitude from visibleMapRect? Or is there a formula to convert those values I am getting in longitude and latitude?

For additional information the longitude and latitude of a visible point is:

103.861463   //longitude
1.3165999    //latitude

when the visibleMapRect is:

211653840.83766   //origin.x
133214809.161136  //origin.y
16865.6084111989  //size.width
26357.4158187151  //size.height

Solution

  • let rect = mapView.visibleMapRect
    let mapPoint = MKMapPointMake(rect.origin.x, rect.origin.y)
    let coordinate = MKCoordinateForMapPoint(mapPoint)
    print(coordinate.latitude)
    print(coordinate.longitude)