How can I add and display multiple circles in different colors inside a map (MKMapView
)? I figured out how to add one circle, but can't figure out how to add multiple circles in various sizes and colors ... any help would be appreciated!
Asked
Active
Viewed 3,417 times
1
1 Answers
3
Here's some code I use to draw two concentric circles at a given location on the map. The outer one is gray, and the inner one is white. (in my example "range" is the circle radius) Both have some transparency:
- (void)drawRangeRings: (CLLocationCoordinate2D) where {
// first, I clear out any previous overlays:
[mapView removeOverlays: [mapView overlays]];
float range = [self.rangeCalc currentRange] / MILES_PER_METER;
MKCircle* outerCircle = [MKCircle circleWithCenterCoordinate: where radius: range];
outerCircle.title = @"Stretch Range";
MKCircle* innerCircle = [MKCircle circleWithCenterCoordinate: where radius: (range / 1.425f)];
innerCircle.title = @"Safe Range";
[mapView addOverlay: outerCircle];
[mapView addOverlay: innerCircle];
}
Then, make sure your class implements the MKMapViewDelegate
protocol, and define how your overlays look in the following method:
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
MKCircle* circle = overlay;
MKCircleView* circleView = [[MKCircleView alloc] initWithCircle: circle];
if ([circle.title compare: @"Safe Range"] == NSOrderedSame) {
circleView.fillColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.25];
circleView.strokeColor = [UIColor whiteColor];
} else {
circleView.fillColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.25];
circleView.strokeColor = [UIColor grayColor];
}
circleView.lineWidth = 2.0;
return circleView;
}
And, of course, don't forget to set the delegate on your MKMapView
object, or the above method will never get called:
mapView.delegate = self;

Nate
- 31,017
- 13
- 83
- 207
-
does this display both circles at the same time? – snksnk Sep 21 '12 at 20:54
-
1yes. they are drawn on top of each other (smaller white circle on top of larger grey circle). obviously, if you want the two (or more) circles to have **different** center locations, you would need to change my code so that the same value is not passed to `circleWithCenterCoordinate:` for both circles. – Nate Sep 21 '12 at 22:44