2

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?

Axel
  • 1,716
  • 1
  • 16
  • 28

2 Answers2

2
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(center_coord, radius, radius);

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

Community
  • 1
  • 1
Craig
  • 8,093
  • 8
  • 42
  • 74
  • Well, in the first moment, I thought that this solved my problem. But the response in the linked answer requires to have a MKOverlayView - which will require the boundingMapRect from above. Circular problem? – Axel Apr 09 '12 at 18:06
0

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));
}
Shmidt
  • 16,436
  • 18
  • 88
  • 136