如何在地图内添加和显示不同颜色的多个圆圈(MKMapView
)?我想出了如何添加一个圈子,但无法弄清楚如何添加各种尺寸和颜色的多个圈子......任何帮助都将不胜感激!
答案 0 :(得分:3)
这是我用来在地图上的给定位置绘制两个同心圆的一些代码。外部是灰色,内部是白色。 (在我的例子中,“范围”是圆半径)两者都有一些透明度:
- (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;