Matthew Simpson
Matthew Simpson

Reputation: 163

OpenLayers: Loading a TileJSON from a local file

I'm trying to implement a local copy of a TileJSON in an iOS app through Cordova. The issue I'm having is that OpenLayers doesn't seem to recognise the JSON file as valid and thus doesn't show any tiles. I have tried local and remote versions of the same TileJSON and looked at the Console Logs, the local one has a status of "error" (but no explanation as to what that error might be...).

I think the issue is down to the fact that the JSON file is being loaded using a file: URL, rather than http:. I have put the JSON file on a remote server and this not only loads fine but actually loads the tiles from the local path.

Can OpenLayers be tricked into accepting the local file as a valid JSON file? Can Cordova be forced to load local files via HTTP? I think either of these options would fix the issue.

Thanks

EDIT: Here's the code I'm using to load the TileJSON:

var mapLayer = new ol.layer.Tile({
    source: new ol.source.TileJSON({
        url: getPhoneGapPath() + 'tiles.json',
        crossOrigin: 'anonymous'
    })
});
this.map.addLayer(mapLayer);

function getPhoneGapPath() {
    var path = window.location.pathname;
    path = path.substr( path, path.length - 10 );
    return path;
}

The getPhoneGapPath() function is used to get the path to the webroot of the Cordova app.

Upvotes: 0

Views: 631

Answers (1)

ahocevar
ahocevar

Reputation: 5648

This is probably related to a bug in OpenLayers, https://github.com/openlayers/ol3/issues/5647. The fix will be in the next release.

Also make sure that you configure Cordova to allow access to file:// urls when the application is served from a file:// url. The equivalent option in Chrome is --allow-file-access-from-files.

Upvotes: 1

Related Questions