对于MKOverlayPathView,我认为添加文本的最简单方法是覆盖drawMapRect:zoomScale:inContext: 并将路径和文本绘制在那里(并且什么都不做或不实现createPath)。
但是,如果您仍然要使用drawMapRect,您可能只想切换到子类化一个普通的MKOverlayView 而不是MKOverlayPathView。
使用MKOverlayView,覆盖drawMapRect:zoomScale:inContext: 方法并使用CGContextAddArc(或CGContextAddEllipseInRect 或CGPathAddArc)绘制圆。
您可以在此方法中使用drawAtPoint 绘制文本,该方法将具有所需的context。
例如:
-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context
{
//calculate CG values from circle coordinate and radius...
CLLocationCoordinate2D center = circle_overlay_center_coordinate_here;
CGPoint centerPoint =
[self pointForMapPoint:MKMapPointForCoordinate(center)];
CGFloat radius = MKMapPointsPerMeterAtLatitude(center.latitude) *
circle_overlay_radius_here;
CGFloat roadWidth = MKRoadWidthAtZoomScale(zoomScale);
//draw the circle...
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextSetFillColorWithColor(context, [[UIColor blueColor] colorWithAlphaComponent:0.2].CGColor);
CGContextSetLineWidth(context, roadWidth);
CGContextAddArc(context, centerPoint.x, centerPoint.y, radius, 0, 2 * M_PI, true);
CGContextDrawPath(context, kCGPathFillStroke);
//draw the text...
NSString *text = @"Hello";
UIGraphicsPushContext(context);
[[UIColor redColor] set];
[text drawAtPoint:centerPoint
withFont:[UIFont systemFontOfSize:(5.0 * roadWidth)]];
UIGraphicsPopContext();
}
关于另一个答案中的评论...
当相关MKOverlay 的中心坐标或半径(或其他)发生变化时,您可以通过在其上调用setNeedsDisplayInMapRect: 来使MKOverlayView“移动”(而不是再次删除和添加覆盖)。 (使用MKOverlayPathView 时,您可以调用invalidatePath。)
调用setNeedsDisplayInMapRect:时,可以将覆盖层的boundingMapRect传递给map rect参数。
在 WWDC 2010 的 LocationReminders 示例应用程序中,覆盖视图使用 KVO 来观察关联 MKOverlay 的变化,并在检测到圆圈属性发生变化时自行移动,但您可以通过其他方式监控变化并调用setNeedsDisplayInMapRect: 显式来自覆盖视图之外。
(在对另一个答案的评论中,我确实提到了使用MKOverlayPathView,这就是LocationReminders 应用程序实现移动圆圈覆盖视图的方式。但我应该提到你也可以使用MKOverlayView 来画一个圆圈。对此感到抱歉。 )