【问题标题】:How to refresh an MKOverlayRenderer when mapView changemapView 更改时如何刷新 MKOverlayRenderer
【发布时间】:2015-05-30 09:15:27
【问题描述】:

我是编码新手,我正在尝试实现类似提醒应用程序:

我已经关注另一个answer 来实现它并

这是我的代码:

在我的视图控制器中:

var circle = MKCircle(centerCoordinate: location.coordinate, radius: 100)
self.mapView.addOverlay(circle)

在我的 MKMapView 中:

func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {

    if overlay is MKCircle
    {
        render = MapFillRenderer(overlay: overlay)
        return render
    } else {
        return nil
    }
}

还有 MapFillRenderer(MKOverlayRenderer 的子类):

class MapFillRenderer: MKOverlayRenderer {

    var colorIn: UIColor
    var colorOut: UIColor

    override func drawMapRect(mapRect: MKMapRect, zoomScale: MKZoomScale, inContext context: CGContext!) {
        // Fill full map rect with some color.
        var rect = self.rectForMapRect(mapRect)
        CGContextSaveGState(context);
        CGContextAddRect(context, rect);
        CGContextSetFillColorWithColor(context, colorOut.CGColor)
        CGContextFillRect(context, rect);
        CGContextRestoreGState(context);

        // Clip rounded hole.
        CGContextSaveGState(context);
        CGContextSetFillColorWithColor(context, colorIn.CGColor);
        CGContextSetBlendMode(context, kCGBlendModeClear);
        CGContextFillEllipseInRect(context, self.rectForMapRect(self.overlay.boundingMapRect))
        CGContextRestoreGState(context);

        // Draw circle
        super.drawMapRect(mapRect, zoomScale: zoomScale, inContext: context)
    }
}

问题:

但是当用户移动地图时,我遇到了一个问题,主蒙版没有刷新,也没有填满整个地图区域。 值得注意的是,它会刷新,但只有当我足够缩小时。 当用户在不缩小地图的情况下移动地图时,如何强制刷新? 我试过了,但失败了:

    func mapView(mapView: MKMapView!, regionDidChangeAnimated animated: Bool) {
    if render.overlay != nil {
        render.setNeedsDisplay()
    }
}

感谢您的任何想法,

这里是用户在不缩放的情况下移动地图时的结果图片:

【问题讨论】:

  • 注意:Reminders Apple App,当您为某个位置添加提醒时,有同样的错误

标签: ios swift mapkit mkoverlay


【解决方案1】:

MapKit 按图块调用您的渲染器。为了确定要渲染哪些图块(以 'MKMapRect's 表示),它会询问 MKOverlay 您的渲染器是否会在该图块上渲染。

MKCircle 的实现方式很可能只对那些包含您的圈子的图块说“是”。

因此您需要覆盖 var boundingMapRect: MKMapRect { get } 以返回 MKMapRectWorld 或覆盖 optional func intersectsMapRect(_ mapRect: MKMapRect) -> BoolMKCircle

然后,您的渲染器会为显示给用户的每个图块调用。

由于 MKCircle 主要用于计算圆周围的矩形并检查图块是否会与该矩形相交,因此最好实现自己的 MKOverlay,只需返回 MKMapRectWorld 作为其 boundingMapRec

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-22
    • 2014-08-22
    • 2014-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多