在iOS中添加多个圆圈

如何添加和显示地图( MKMapView )内的不同颜色的多个圈子? 我想出了如何添加一个圆,但不知道如何添加多个不同大小和颜色的圆圈…任何帮助将不胜感激!

下面是我用来在地图上给定位置绘制两个同心圆的代码。 外面的是灰色的,内面的是白色的。 (在我的例子中“范围”是圆的半径)两者都有一些透明度:

 - (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]; } 

然后,确保你的类实现了MKMapViewDelegate协议,并且定义你的覆盖层在下面的方法中的外观:

 - (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; } 

当然,不要忘记在你的MKMapView对象上设置委托,否则上面的方法永远不会被调用:

 mapView.delegate = self;