では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
ことができます。( を使用する場合は、代わりに呼び出すことができます。)MKOverlayView
setNeedsDisplayInMapRect:
MKOverlayPathView
invalidatePath
を呼び出すときに、map rect パラメータのオーバーレイの を setNeedsDisplayInMapRect:
渡すことができます。boundingMapRect
WWDC 2010 の LocationReminders サンプル アプリでは、オーバーレイ ビューは KVO を使用して関連付けられた へのMKOverlay
変更を監視し、円のプロパティへの変更を検出するたびに移動しますが、他の方法で変更を監視しsetNeedsDisplayInMapRect:
、オーバーレイ ビューの外部から明示的に呼び出すことができます。 .
(別の回答へのコメントで、使用について言及しましMKOverlayPathView
たが、それが LocationReminders アプリが移動する円のオーバーレイ ビューを実装する方法です。しかし、使用MKOverlayView
して円を描く方法についても言及する必要がありました。申し訳ありません。)