如何从子类调用 MKTileOverlay "url" 函数?获得 EXC_BAD_INSTRUCTION

how to call MKTileOverlay "url" function from subclass? getting EXC_BAD_INSTRUCTION

我在下面标记的位置遇到运行时错误?如何从子类调用 MKTileOverlay "url" 函数?得到 EXC_BAD_INSTRUCTION?

基本上想在某些地方显示自定义图块,但在不可用时返回到标准 Apple 地图图块。

class GCMapOverlay : MKTileOverlay {
    override func url(forTilePath path: MKTileOverlayPath) -> URL {
        // Get local custom map tile if available
        let optionalUrl = Bundle.main.url(
            forResource: "\(path.y)",
            withExtension: "png",
            subdirectory: "tiles/\(path.z)/\(path.x)",
            localization: nil)
        NSLog("tiles/\(path.z)/\(path.x)/\(path.y)")

        guard let url = optionalUrl else {
            // Local tile not available - want to drop back to an apple maps tile (as if MKTileOverlay wasn't subclassed)
            return super.url(forTilePath: path)    // <== RUNTIME ERROR: NetworkLoad (10): EXC_BAD_INSTRUCTION
        }

        // Local tile available so return
        return url
    }
}

在我的控制器中

func setupTileRenderer() {
    let overlay = GCMapOverlay()
    overlay.canReplaceMapContent = true
    mapView.addOverlay(overlay, level: .aboveLabels)
    tileRenderer = MKTileOverlayRenderer(tileOverlay: overlay)
}

我从未使用过 MKTileOverlayurl(forTilePath:) 的文档指出:

The default implementation of this method uses the template string you provided at initialization time to build a URL to the specified tile image.

并且 MKTileOverlay class 提供初始化程序:

init(urlTemplate:)

但是当您创建 GCMapOverlay 的实例时,您不会使用该初始化程序。

正在替换:

let overlay = GCMapOverlay()

与:

let overlay = GCMapOverlay(urlTemplate: someAppropriateTemplate)

或在您的子 class 中覆盖 urlTemplate 属性 应该可以解决您在调用 super.url(forTilePath:).

时遇到的问题

删除 overlay.canReplaceMapContent = true 并更改您的 guard 语句,使其加载一个清晰的 256x256 图块。

guard let url = optionalUrl else {
    return Bundle.main.url(forResource: "emptyTile", withExtension: "png")!
}

由于您所有的自定义图块都已存储在本地,因此它们将立即加载到默认的 Apple 图块上,这使得 canReplaceMapContent 没有必要。

确保您的自定义图块中没有 alpha,否则 Apple 图块将在下方可见。