boundingMapRect for a circle

1.6k views Asked by At

For an MKMapView overlay, I need to calculate the boundingMapRect for a circle. I have its center coordinate and radius (in meters) - but no idea how to get the corresponding boundingMapRect. As workaround, I create a MKCircle with the same data and use the boundingMapRect from this. But ... that's not elegant, at least.

So: how to calculate a boundingMapRect for a circle with given radius and center?

2

There are 2 answers

1
Craig On
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(center_coord, radius, radius);

Then use the answer over here to convert MKCoordinateRegion to MKMapRect Convert MKCoordinateRegion to MKMapRect

0
Shmidt On

Craig's answer worked for me:

- (MKMapRect)boundingMapRect
{
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.coordinate, self.radius, self.radius);
    MKMapRect boundingRect = MKMapRectForCoordinateRegion(region);
    return boundingRect;
}

MKMapRect MKMapRectForCoordinateRegion(MKCoordinateRegion region)
{
    MKMapPoint a = MKMapPointForCoordinate(CLLocationCoordinate2DMake(
                                                                      region.center.latitude + region.span.latitudeDelta / 2,
                                                                      region.center.longitude - region.span.longitudeDelta / 2));
    MKMapPoint b = MKMapPointForCoordinate(CLLocationCoordinate2DMake(
                                                                      region.center.latitude - region.span.latitudeDelta / 2,
                                                                      region.center.longitude + 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));
}