ios - MKTileOverlay:如何重新加载特定图 block 或区域

标签 ios mapkit mktileoverlay

我正在开发一个基于 map 的应用程序。我用了MKTileOverlay显示一些动态内容。有时,当新信息到达时,某些图 block 需要重新加载。

我尝试调用overlayRenderer.setNeedsDisplayInMapRect(mapRect)在主线程中,但显然在我的情况下它不会触发 map 重绘。到目前为止,唯一有效的方法是 overlayRenderer.reloadData() 。但是,这将导致重新加载整个内容(而不是仅特定区域)并导致 View 闪烁,因此这不适合我。

有人可以就此提供任何建议吗?谢谢并干杯。

最佳答案

我终于明白了。事实上,叠加层未更新并不是由于 setNeedsDisplayInMapRect 造成的。经过几次检查后,我发现 setNeedsDisplayInMapRect 实际上导致了 DrawMapRect:zoomScale:inContext: 的运行,但是,默认重绘会产生相同的图 block 图像。我猜这可能是由于 MKTileOverlayRender 的一些内部缓存所致。

对我来说,解决方案是对 MKTileOverlayRender 进行子类化,并在新类中:

override func canDrawMapRect(mapRect: MKMapRect, zoomScale: MKZoomScale) -> Bool {

    let tilePath = self.tilePathForMapRect(mapRect, andZoomScale: zoomScale)
    let tilePathString = stringForTilePath(tilePath)
    if let _ = self.cache.objectForKey(tilePathString) {
        return true
    } else {
        let tileOverlay = self.overlay as! MKTileOverlay
        tileOverlay.loadTileAtPath(tilePath, result: {
            data, error in
            if error == nil && data != nil {
                if let image = UIImage(data: data!) {
                    self.cache.setObject(image, forKey: tilePathString)
                    self.setNeedsDisplayInMapRect(mapRect, zoomScale: zoomScale)
                }
            }
        })

        return false
    }
}

override func drawMapRect(mapRect: MKMapRect, zoomScale: MKZoomScale, inContext context: CGContext) {
    let tilePath = self.tilePathForMapRect(mapRect, andZoomScale: zoomScale)
    let tilePathString = stringForTilePath(tilePath)

    if let image = self.cache.objectForKey(tilePathString) as? UIImage {
        CGContextDrawImage(context, self.rectForMapRect(mapRect), image.CGImage)
    } else {
        super.setNeedsDisplayInMapRect(mapRect, zoomScale: zoomScale)
    }

    self.cache.removeObjectForKey(tilePathString)
}

关于ios - MKTileOverlay:如何重新加载特定图 block 或区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38081372/

相关文章:

ios - “an ssl error has occurred and a secure connection to the server cannot be made” 仅在手机上连接到内部开发服务器

ios - 如何使用 MKDirections 请求支持多种传输类型

ios - MKMapView init 在包含 Google Maps SDK 的应用程序中失败

ios - 重新加载 map 注释时出现问题

objective-c - map 套件 : add MKPolyline hide MKTileOverlay

ios - 自定义磁贴未从文档文件夹加载

ios - 更改xi​​b名称

javascript - 是否可以检测是否安装了whatsapp?

swift - 如何从子类调用 MKTileOverlay "url"函数?获取 EXC_BAD_INSTRUCTION

ios - i0S7 和 iOS5 中的 UITableView 大小