use*_*026 5 mkmapview ios mkoverlay
我有一个从MKOverlayPathView继承的地图自定义视图.我需要这个自定义视图来显示圆,线和文本.
我已经设法使用路径绘制CGPathAddArc和CGPathAddLineToPoint函数绘制圆和线.
但是,我仍然需要添加文字.
我尝试使用添加文本
[text drawAtPoint:centerPoint withFont:font];
Run Code Online (Sandbox Code Playgroud)
但我得到了无效的上下文错误.
任何的想法?
小智 8
有了MKOverlayPathView,我认为添加文本最简单的方法是覆盖drawMapRect:zoomScale:inContext:并将路径和文本绘制放在那里(并且不执行任何操作或不实现createPath).
但是如果你打算继续使用drawMapRect,你可能只想切换到子类MKOverlayView而不是MKOverlayPathView.
使用a 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();
}
Run Code Online (Sandbox Code Playgroud)
关于另一个答案中的评论......
当相关的中心坐标或半径(或其他)MKOverlay发生变化时,您可以MKOverlayView通过调用setNeedsDisplayInMapRect:它来"移动" (而不是再次移除和添加叠加).(使用时MKOverlayPathView,你可以打电话invalidatePath.)
调用时setNeedsDisplayInMapRect:,您可以传递boundingMapRectmap rect参数的叠加层.
在WWDC 2010的LocationReminders示例应用程序中,叠加视图使用KVO观察对关联的更改,MKOverlay并在检测到对圆的属性进行更改时自行移动,但您可以通过其他方式监视更改并setNeedsDisplayInMapRect:从叠加视图外部明确调用.
(在我提到的另一个答案的评论中MKOverlayPathView,这就是LocationReminders应用程序如何实现移动圆圈叠加视图.但我应该提到你如何使用MKOverlayView画圆圈.抱歉.)
| 归档时间: |
|
| 查看次数: |
4094 次 |
| 最近记录: |