Mapbox RMTileCacheBackgroundDelegate回调无效

时间:2015-03-22 21:25:47

标签: ios swift mapbox

我有下面的快捷代码。地图上的所有内容都按预期工作,但我从未看到任何RMTileCashBackgroundDelegate方法被调用。我所看到的只是"缓存开始了!"然后什么也没有。我已经尝试了很多调整,但无法实现这一目标。任何帮助将不胜感激!

class MapboxViewController: UIViewController, RMMapViewDelegate, RMTileCacheBackgroundDelegate {
    var mapView: RMMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let mapSource:RMMapboxSource = RMMapboxSource(mapID: "myMapId")

        mapView = RMMapView(frame: self.view.bounds, andTilesource: mapSource)
        mapView.delegate = self

        //background caching
        mapView.tileCache.backgroundCacheDelegate = self
        mapView.tileCache.beginBackgroundCacheForTileSource(mapView.tileSource, southWest: swBounds, northEast: neBounds, minZoom: 13, maxZoom: 17)
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        mainView.addSubview(mapView)
    }

    func tileCache(tileCache: RMTileCache!, didBeginBackgroundCacheWithCount tileCount: Int, forTileSource tileSource: RMTileSource) {
        println("Caching started!")
    }
    func tileCache(tileCache: RMTileCache!, didBackgroundCacheTile tile: RMTile, withIndex tileIndex: Int, ofTotalTileCount totalTileCount: Int) {
        println("Cached tile \(tileIndex) of \(totalTileCount)")
    }
    func tileCache(tileCache: RMTileCache!, didReceiveError error: NSError!, whenCachingTile tile: RMTile) {
        println("Error caching tile")
    }
    func tileCacheDidCancelBackgroundCache(tileCache: RMTileCache!) {
        println("Caching cancelled!")
    }
    func tileCacheDidFinishBackgroundCache(tileCache: RMTileCache!) {
        println("Caching complete!")
    }
}

1 个答案:

答案 0 :(得分:0)

这里有两个问题,一个在上面的代码中并不明显:

  1. tileCache函数采用UInt而不是Int(强制一旦我升级到Swift 1.2)
  2. 我对beginBackgroundCacheForTileSource中的边界框进行了舍入,以便最终得到一个没有tile的边界。这对我来说很草率,实际上导致了Mapbox iOS SDK的崩溃。
  3. 感谢Mapbox对他们的帮助。这是Swift中一个不错的离线缓存示例供参考:https://github.com/mapbox/mapbox-ios-sdk-offline