1

如何在地图 ( MKMapView) 内添加和显示多个不同颜色的圆圈?我想出了如何添加一个圆圈,但无法弄清楚如何添加不同大小和颜色的多个圆圈......任何帮助将不胜感激!

4

1 回答 1

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;
于 2012-09-21T20:48:42.037 回答