无法在MapKit覆盖层视图上描绘路径

5

我正在使用iOS 4上的iPhone上的mapkit。 我正在使用自定义覆盖层和自定义覆盖层视图,在地图上绘制形状。 目前,这些形状只是矩形,但我计划使用更复杂的形状。这就是为什么我不使用MKPolygon覆盖类型的原因。 以下是我的覆盖层视图绘制方法的代码:

-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context
{
    // Clip context to bounding rectangle
    MKMapRect boundingMapRect = [[self overlay] boundingMapRect];
    CGRect boundingRect = [self rectForMapRect:boundingMapRect];
    CGContextAddRect(context, boundingRect);
    CGContextClip(context);

    // Define shape
    CGRect shapeRect = CGRectMake(0.5f, 0.5f, boundingRect.size.width - 1.0f, boundingRect.size.height - 1.0f);

    // Fill
    CGContextSetRGBFillColor(context, 0.5f, 0.5f, 0.5f, 0.5f);
    CGContextFillRect(context, shapeRect);

    // Stroke
    CGContextSetRGBStrokeColor(context, 0, 0, 0, 0.75f);
    CGContextSetLineWidth(context, 1.0f);
    CGContextStrokeRect(context, shapeRect);
}

问题在于矩形被正确填充(因此其边界矩形的设置似乎是正确的),但它们没有被描边。

有人能帮忙吗?

谢谢!


虽然我不知道解决方案,但我想提醒您关注CGRectInset(而不是使用CGRectMake创建矩形)。 - DarkDust
刚想到一个问题:如果你进一步缩小矩形,它会被描边吗?所以首先你填充它,然后再将矩形缩小0.5,最后描边它? - DarkDust
1
发现另一个与此相关的问题:https://dev59.com/NVXTa4cB1Zd3GeqPyiCw,因此似乎问题出在线宽度缩放上,看起来我必须通过以下方式修复我的代码:CGContextSetLineWidth(context, 5.0f / zoomScale); - Giorgio Barchiesi
我还发现边界矩形的原点不是0, 0,所以我的代码需要另一个修复:CGRect shapeRect = CGRectMake(boundingRect.origin.x + 0.5f, boundingRect.origin.y + 0.5f, boundingRect.size.width - 1.0f, boundingRect.size.height - 1.0f); - Giorgio Barchiesi
然后您应该编写一个描述解决方案的答案并接受它。 - DarkDust
@DarkDust关于CGRectInset是正确的,所以代码如下:CGRect shapeRect = CGRectInset(boundingRect, 2.5f / zoomScale, 2.5f / zoomScale); 这样插图也会被正确地缩放!让我再测试一下,然后我会写一个答案。谢谢。 - Giorgio Barchiesi
2个回答

2

正如之前某些评论所述,问题在于线宽。更一般地,所有绘图都会自动缩放以跟随地图缩放,因此如果您希望某些绘图指标与缩放无关,则必须将其除以缩放比例。

以下是新代码,在我的iPhone 4上可以正常工作:

-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context
{
    // Clip context to bounding rectangle
    MKMapRect boundingMapRect = [[self overlay] boundingMapRect];
    CGRect boundingRect = [self rectForMapRect:boundingMapRect];
    CGContextAddRect(context, boundingRect);
    CGContextClip(context);

    // Define shape
    CGRect shapeRect = CGRectInset(boundingRect, 2.0f / zoomScale, 2.0f / zoomScale);

    // Fill
    CGContextSetRGBFillColor(context, 0.5f, 0.5f, 0.5f, 0.5f);
    CGContextFillRect(context, shapeRect);

    // Stroke
    CGContextSetRGBStrokeColor(context, 0, 0, 0, 0.75f);
    CGContextSetLineWidth(context, 4.0f / zoomScale);
    CGContextStrokeRect(context, shapeRect);
}

我还会汇报我在叠加层中使用的代码来计算和返回边界矩形,因为我认为这可以帮助:

-(MKMapRect)boundingMapRect
{
    // Overlay bounds
    CLLocationCoordinate2D topLeftcoordinate = <the top-left coordinate of overlay>;
    CLLocationCoordinate2D bottomRightCoordinate = <the bottom-right coordinate of overlay>;

    // Convert them to map points
    MKMapPoint topLeftPoint = MKMapPointForCoordinate(topLeftcoordinate);
    MKMapPoint bottomRightPoint = MKMapPointForCoordinate(bottomRightCoordinate);

    // Calculate map rect
    return MKMapRectMake(topLeftPoint.x, topLeftPoint.y, bottomRightPoint.x - topLeftPoint.x, topLeftPoint.y - bottomRightPoint.y);
}

感谢大家的评论和建议。

0
  1. 目前,我们应该使用MKOverlayRenderer而不是MKOverlayView。
  2. 在方法-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context中, 使用scaleFactor来配置线条的描边宽度或其他可能受到地图内容比例影响的属性。

请参考苹果官方网站drawMapRect:zoomScale:inContext:上关于MKOverlayRenderer的说明。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接