From ba5af162af694fc1d767816ed19cf180e2a0b943 Mon Sep 17 00:00:00 2001 From: Code WvS Date: Sun, 22 Mar 2015 18:04:25 +0100 Subject: fix caching on mobile --- snap.html | 52 +++++++++++++++++++++++----------------------------- 1 file changed, 23 insertions(+), 29 deletions(-) (limited to 'snap.html') diff --git a/snap.html b/snap.html index 4a7d5d1..9b0cafe 100755 --- a/snap.html +++ b/snap.html @@ -95,44 +95,38 @@ }); }); + var state = "mobilewifi"; // TODO: check other states than listed below // TODO: too much repeated code, shorten the url part if ("connection" in navigator) { // mobile cordova app - console.log('DEBUG - detected mobile'); state = navigator.connection.type; - // download everything & in high resolution - if (state == Connection.ETHERNET || state == Connection.WIFI) { - layer = L.tileLayer('http://api.tiles.mapbox.com/v4/mapbox.streets/{z}/{x}/{y}@2x.png?access_token=pk.eyJ1IjoiY29kZS13dnMiLCJhIjoielU1N2dQayJ9.miUjtJaW7Y_Xx46dC6m0LA', { - useCache: true, - cacheMaxAge: 604800000, // one week - attribution: '© OpenStreetMap contributors' - }); - new OSMBuildings(map).load(); - // download low-res and cache as much as possible - // we do not need OSMBuildings here - } else if (state == Connection.CELL) { - layer = L.tileLayer('http://api.tiles.mapbox.com/v4/mapbox.streets/{z}/{x}/{y}.jpg70?access_token=pk.eyJ1IjoiY29kZS13dnMiLCJhIjoielU1N2dQayJ9.miUjtJaW7Y_Xx46dC6m0LA', { - useCache: true, - cacheMaxAge: 604800000, // one week - attribution: '© OpenStreetMap contributors' - }); - // shit, we're offline - let's hope there is something cached - // OSMBuildings won't work, anyway - } else { - layer = L.tileLayer('http://api.tiles.mapbox.com/v4/mapbox.streets/{z}/{x}/{y}.jpg70?access_token=pk.eyJ1IjoiY29kZS13dnMiLCJhIjoielU1N2dQayJ9.miUjtJaW7Y_Xx46dC6m0LA', { - useCache: true, - useOnlyCache: true, - cacheMaxAge: 604800000, // one week - attribution: '© OpenStreetMap contributors' - }); - } - } else { - // desktop + } + console.log('Using tile layer appropriate for ' + state); + // download everything and in high resolution + if (state == 'mobilewifi') { layer = L.tileLayer('http://api.tiles.mapbox.com/v4/mapbox.streets/{z}/{x}/{y}@2x.png?access_token=pk.eyJ1IjoiY29kZS13dnMiLCJhIjoielU1N2dQayJ9.miUjtJaW7Y_Xx46dC6m0LA', { useCache: true, + cacheMaxAge: 604800000, // one week attribution: '© OpenStreetMap contributors' }); new OSMBuildings(map).load(); + // download low-res and cache as much as possible + // we do not need OSMBuildings here + } else if (state == "mobilecellular") { + layer = L.tileLayer('http://api.tiles.mapbox.com/v4/mapbox.streets/{z}/{x}/{y}.jpg70?access_token=pk.eyJ1IjoiY29kZS13dnMiLCJhIjoielU1N2dQayJ9.miUjtJaW7Y_Xx46dC6m0LA', { + useCache: true, + cacheMaxAge: 604800000, // one week + attribution: '© OpenStreetMap contributors' + }); + // shit, we're offline - let's hope there is something cached + // OSMBuildings won't work, anyway + } else { + layer = L.tileLayer('http://api.tiles.mapbox.com/v4/mapbox.streets/{z}/{x}/{y}.jpg70?access_token=pk.eyJ1IjoiY29kZS13dnMiLCJhIjoielU1N2dQayJ9.miUjtJaW7Y_Xx46dC6m0LA', { + useCache: true, + useOnlyCache: true, + cacheMaxAge: 604800000, // one week + attribution: '© OpenStreetMap contributors' + }); } layer.addTo(map); L.control.coordinates({ -- cgit v1.3.1