diff options
| author | Code WvS <code-wvs@quantentunnel.de> | 2015-03-23 20:07:26 +0100 |
|---|---|---|
| committer | Code WvS <code-wvs@quantentunnel.de> | 2015-03-23 20:07:26 +0100 |
| commit | 0ab6d5484b093a58f5f37286e17834dfd54ea724 (patch) | |
| tree | 3e1faf6bebcc2b96e7608f50a63aeb6426b0014d | |
| parent | 4983e0ad91dc0d8dea6e79754e83f0c52e7ed5d2 (diff) | |
| download | snap-yow-0ab6d5484b093a58f5f37286e17834dfd54ea724.tar.gz snap-yow-0ab6d5484b093a58f5f37286e17834dfd54ea724.zip | |
fix zooming and improve caching
| -rwxr-xr-x | snap.html | 49 |
1 files changed, 33 insertions, 16 deletions
@@ -83,7 +83,6 @@ // initialize leaflet map = L.map('map', { - maxZoom: 21, keyboard: false, closePopupOnClick: false }); @@ -96,40 +95,58 @@ }); }); - var state = "wifi"; // TODO: check other states than listed below + var state = 'desktop'; // TODO: check other states than listed below // TODO: too much repeated code, shorten the url part - if ("connection" in navigator) { + if ('connection' in navigator) { // mobile cordova app state = navigator.connection.type; } console.log('Using tile layer appropriate for ' + state); - // download everything and in high resolution - if (state == 'wifi') { - layer = L.tileLayer('http://api.tiles.mapbox.com/v4/mapbox.streets/{z}/{x}/{y}@2x.png?access_token=pk.eyJ1IjoiY29kZS13dnMiLCJhIjoielU1N2dQayJ9.miUjtJaW7Y_Xx46dC6m0LA', { + + // mobile resolution is JPG 70% (smallest) + var url = 'http://api.tiles.mapbox.com/v4/mapbox.streets/{z}/{x}/{y}.jpg70?access_token=pk.eyJ1IjoiY29kZS13dnMiLCJhIjoielU1N2dQayJ9.miUjtJaW7Y_Xx46dC6m0LA'; + var attribution = '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'; + + if (state == 'desktop') { + // download everything and in high resolution + layer = L.tileLayer('http://api.tiles.mapbox.com/v4/mapbox.streets/{z}/{x}/{y}@2x.jpg?access_token=pk.eyJ1IjoiY29kZS13dnMiLCJhIjoielU1N2dQayJ9.miUjtJaW7Y_Xx46dC6m0LA', { + useCache: true, + cacheMaxAge: 604800000, // one week + maxZoom: 21, + maxNativeZoom: 19, + attribution: attribution + }); + new OSMBuildings(map).load(); + } else if (state == 'wifi') { + // download low-res and cache as much as possible + // with wifi, fancy buildings are ok + layer = L.tileLayer(url, { useCache: true, cacheMaxAge: 604800000, // one week + maxZoom: 21, maxNativeZoom: 19, - attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors' + attribution: attribution }); new OSMBuildings(map).load(); - // download low-res and cache as much as possible - // we do not need OSMBuildings here - } else if (state == "cellular") { - layer = L.tileLayer('http://api.tiles.mapbox.com/v4/mapbox.streets/{z}/{x}/{y}.jpg70?access_token=pk.eyJ1IjoiY29kZS13dnMiLCJhIjoielU1N2dQayJ9.miUjtJaW7Y_Xx46dC6m0LA', { + } else if (state == 'cellular') { + // we do not need OSMBuildings here + layer = L.tileLayer(url, { useCache: true, cacheMaxAge: 604800000, // one week + maxZoom: 21, maxNativeZoom: 19, - attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors' + attribution: attribution }); - // 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', { + // shit, we're offline - let's hope there is something cached + // OSMBuildings won't work, anyway + layer = L.tileLayer(url, { useCache: true, useOnlyCache: true, cacheMaxAge: 604800000, // one week + maxZoom: 21, maxNativeZoom: 19, - attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors' + attribution: attribution }); } layer.addTo(map); |
