Convert MKCoordinateRegion to MKMapRect Convert MKCoordinateRegion to MKMapRect ios ios

Convert MKCoordinateRegion to MKMapRect

To add another implementation to the pile:

- (MKMapRect)MKMapRectForCoordinateRegion:(MKCoordinateRegion)region{    MKMapPoint a = MKMapPointForCoordinate(CLLocationCoordinate2DMake( + region.span.latitudeDelta / 2, - region.span.longitudeDelta / 2));    MKMapPoint b = MKMapPointForCoordinate(CLLocationCoordinate2DMake( - region.span.latitudeDelta / 2, + region.span.longitudeDelta / 2));    return MKMapRectMake(MIN(a.x,b.x), MIN(a.y,b.y), ABS(a.x-b.x), ABS(a.y-b.y));}

NB: There are many ways to convert between MKMapRect and MKCoordinateRegion. This one certainly is not the exact inverse of MKCoordinateRegionMakeWithDistance(), but approximates it fairly well. So, be careful converting back and forth, because information can be lost.

This is a Swift version to Leo & Barnhart solution

func MKMapRectForCoordinateRegion(region:MKCoordinateRegion) -> MKMapRect {    let topLeft = CLLocationCoordinate2D(latitude: + (region.span.latitudeDelta/2), longitude: - (region.span.longitudeDelta/2))    let bottomRight = CLLocationCoordinate2D(latitude: - (region.span.latitudeDelta/2), longitude: + (region.span.longitudeDelta/2))    let a = MKMapPointForCoordinate(topLeft)    let b = MKMapPointForCoordinate(bottomRight)        return MKMapRect(origin: MKMapPoint(x:min(a.x,b.x), y:min(a.y,b.y)), size: MKMapSize(width: abs(a.x-b.x), height: abs(a.y-b.y)))}

Use MKMapPointForCoordinate to convert the 2 point of the region (top/left and bottom/right), then create the MKMapRect using the 2 MKMapPoints

        CLLocationCoordinate2D coordinateOrigin = CLLocationCoordinate2DMake(latitude, longitude);        CLLocationCoordinate2D coordinateMax = CLLocationCoordinate2DMake(latitude + cellSize, longitude + cellSize);        MKMapPoint upperLeft = MKMapPointForCoordinate(coordinateOrigin);        MKMapPoint lowerRight = MKMapPointForCoordinate(coordinateMax);        MKMapRect mapRect = MKMapRectMake(upperLeft.x,                                          upperLeft.y,                                          lowerRight.x - upperLeft.x,                                          lowerRight.y - upperLeft.y);