I'm developing an app with turn-by-turn navigation and I use OpenLayers consuming a tile server. However, I need to cache all the tiles of the route to be able to navigate offline. Is there a way to get all the tiles based on a LineString?

Currently just caching the tiles from the current position, but I need to get all the tiles in the route. I tried the following approach I found in the documentation, but as I understand it it is only caching only the seed current extent.

// start seeding the cache
    function startSeeding() {
        var layer = map.baseLayer,
            zoom = map.getZoom();
        seeding = {
            zoom: zoom,
            extent: map.getExtent(),
            center: map.getCenter(),
            cacheWriteActive: cacheWrite.active,
            buffer: layer.buffer,
            layer: layer
        // make sure the next setCenter triggers a load
        map.zoomTo(zoom === layer.numZoomLevels-1 ? zoom - 1 : zoom + 1);
        // turn on cache writing
        // turn off cache reading

        layer.events.register("loadend", null, seed);

        // start seeding
        map.setCenter(seeding.center, zoom);

function seed() {
        var layer = seeding.layer;
        var tileWidth = layer.tileSize.w;
        var nextZoom = map.getZoom() + 1;
        var extentWidth = seeding.extent.getWidth() / map.getResolutionForZoom(nextZoom);
        // adjust the layer's buffer size so we don't have to pan
        layer.buffer = Math.ceil((extentWidth / tileWidth - map.getSize().w / tileWidth) / 2);
        if (nextZoom === layer.numZoomLevels-1) {

    // stop seeding (when done or when cache is full)
    function stopSeeding() {
        // we're done - restore previous settings
        seeding.layer.events.unregister("loadend", null, seed);
        seeding.layer.buffer = seeding.buffer;
        map.setCenter(seeding.center, seeding.zoom);
        if (!seeding.cacheWriteActive) {
        if (read.checked) {
        seeding = false;
  • 1
    Welcome to GIS SE! Can you provide details as to what you have tried already?
    – whyzar
    May 26 '20 at 13:42
  • @whyzar I added that I already tried to do it but it doesn’t completely solve the problem because it’s necessary to take the entire route May 26 '20 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.