在iOS中围绕图钉添加多个圆圈 [英] adding multiple circles around a pin in iOS
问题描述
如何在地图(MKMapView
)中添加并显示不同颜色的多个圆?我想出了如何添加一个圆圈,但无法弄清楚如何添加各种大小和颜色的多个圆圈...任何帮助将不胜感激!
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!
推荐答案
以下是一些我用来在地图上给定位置绘制两个同心圆的代码.外层是灰色,内层是白色. (在我的示例中,范围"是圆半径)两者都具有一定的透明度:
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];
}
然后,确保您的类实现了MKMapViewDelegate
协议,并通过以下方法定义叠加层的外观:
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;
}
当然,不要忘记在您的MKMapView
对象上设置委托,否则上述方法将永远不会被调用:
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;
这篇关于在iOS中围绕图钉添加多个圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!