gpt4 book ai didi

ios - 如何在 mapView 更改时刷新 MKOverlayRenderer

转载 作者:可可西里 更新时间:2023-11-01 01:05:52 24 4
gpt4 key购买 nike

我是代码新手,我正在尝试实现类似 Reminders 应用程序的功能:

enter image description here

我关注了另一个 answer实现它并

这里是我的代码:

在我的 ViewController 中:

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)
}
}

问题:

但我遇到了一个问题,当用户移动 map 时,主 mask 不会刷新,也不会填满所有 map 区域。值得注意的是,它会刷新,但只有当我足够缩小时才会刷新。当用户移动 map 而不缩小时,如何强制刷新?我尝试过,但失败了:

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

谢谢你的想法,

这里是用户在没有缩放的情况下移动 map 时的结果图像:

enter image description here

最佳答案

MapKit 按图 block 调用您的渲染器。为了确定要渲染哪些图 block (在“MKMapRect”中表示),它会询问 MKOverlay 您的渲染器是否会在该图 block 上进行渲染。

MKCircle 的实现方式很可能只对包含您的圈子的那些图 block 说"is"。

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

然后,为向用户显示的每个图 block 调用您的渲染器。

由于 MKCircle 主要是关于计算围绕圆的矩形并检查图 block 是否与该矩形相交,因此最好实现您自己的 MKOverlay刚回来MKMapRectWorld作为其 boundingMapRec .

关于ios - 如何在 mapView 更改时刷新 MKOverlayRenderer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30544354/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com