Search code examples
iosmapkitcllocationcllocationcoordinate2dcllocationdistance

Reverse function of MKCoordinateRegionMakeWithDistance?


MapKit's built in function MKCoordinateRegionMakeWithDistance takes distances in meters and turns them into a MKCoordinateRegion:

func MKCoordinateRegionMakeWithDistance(
    _ centerCoordinate: CLLocationCoordinate2D, 
    _ latitudinalMeters: CLLocationDistance, 
    _ longitudinalMeters: CLLocationDistance) 
        -> MKCoordinateRegion

is there a reverse function that takes a MKCoordinateRegion and gives me latitudinalMeters and longitudinalMeters?


Solution

  • MKCoordinateRegion gives a center (latitude and longitude) and span (delta latitude and longitude). Given these values, you can determine the location of the edges of the region in latitude and longitude. Once you do that, you can use the haversine formula to obtain latitudinal and longitudinal distances, and you already know the center. In fact, CLLocation has a function distanceFromLocation:(const CLLocation *)location which you should use to avoid a direct implementation of the formula.