Navigate through a pre-known map offline using Phonegap

社会主义新天地 提交于 2019-12-03 16:31:25

I've been working on caching Open Street Map tiles with phonegap using OpenLayers. I store the tiles on the filesystem using PhoneGap-Downloader (https://github.com/phonegap/phonegap-plugins/tree/master/Android/Downloader) and map the url of the tile to the location on the filesystem using localstorage. In openlayers I subclass OpenLayers.Layer.OSM to overload getURLasync and intercept the setting of the tile URL:

EDIT: In recent versions of phonegap there is no need for the PhoneGap-Downloader plugin, just use the native filetransfer: http://docs.phonegap.com/en/2.3.0/cordova_file_file.md.html#FileTransfer_download

OSMWithLocalStorage = OpenLayers.Class(OpenLayers.Layer.OSM, {
    initialize: function(options) {
        OpenLayers.Layer.OSM.prototype.initialize.apply(this, ["CachedMap"]);
        this.async = true;
        this.isBaseLayer = true;

        this.url = 'http://tile.openstreetmap.org/${z}/${x}/${y}.png';
    },
    getURLasync: function(bounds, scope, prop, callback) {
        var url = OpenLayers.Layer.OSM.prototype.getURL.apply(this, [bounds]);
        var cached = window.localStorage.get(url);

        if(cached){
            scope[prop] = cached;
        }
        else{
            scope[prop] = url;
        }

        callback.apply(scope);
    },
});

FWIW, pre-caching tiles is against the Google Maps TOS: http://code.google.com/apis/maps/terms.html

Your best bet would be to use OpenStreetMaps along with one of the libraries, such as MapsForge, to cache the tiles ahead of time.

Edit: Using gmh04's code above, we're using the MapsForge library to cache a set of OSM tiles, then incorporating their cache code into a PhoneGap plugin that will serve back the image tiles in base64 image format (ie "data:image/png;base64,{img data}"). Alter his getURLAsync method to call a plugin instead of referencing localstorage, works great so far.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!