Search code examples
iphoneioscore-graphicsmapkitmkoverlay

Convert MKCircle Radius to CoreGraphics Arc Radius


How can I convert an MKCircle's radius (in meters) into a value I can use to draw a circle using core graphics in an MKOverlayPathView subclass?

In the following code the radius is hardcoded to 50 but I need it to reflect the radius of the MKCircle.

For the position I use MKMapPointForCoordinate() to convert the MKCircle's coordinate to an MKMapPoint, then convert the MKMapPoint to a point using MKOverlayPathView's pointForMapPoint:. But how can I convert the MKCircle's radius into a relative distance?

MKCircle *circle = [self circleForLocation:location]; 
CGPoint relativePoint = [self pointForMapPoint:MKMapPointForCoordinate(circle.coordinate)];
float radius = 50;

//Fill
[self applyFillPropertiesToContext:context atZoomScale:zoomScale];
CGContextAddArc(context, relativePoint.x, relativePoint.y, radius, 0, 2*3.1415926535898, 1);
CGContextDrawPath(context, kCGPathFill);

Solution

  • The height and width of the MKCircle's boundingMapRect should be equal to the circle's diameter:

    CGRect boundingRect = [self rectForMapRect:circle.boundingMapRect];
    CGFloat radius = boundingRect.size.width / 2;