Simplified example.

This commit is contained in:
ahocevar
2012-03-09 19:07:20 +01:00
parent 51a11e053f
commit 5f48e546bc

View File

@@ -3,25 +3,24 @@ function init(){
map = new OpenLayers.Map({ map = new OpenLayers.Map({
div: "map", div: "map",
projection: "EPSG:900913", projection: "EPSG:900913",
layers: [new OpenLayers.Layer.OSM("OpenStreetMap (CORS)", null, { layers: [
eventListeners: { new OpenLayers.Layer.OSM("OpenStreetMap (CORS)", null, {
loadend: updateLayerInfo, eventListeners: {
tileloaded: updateTileInfo, tileloaded: updateStatus,
tileerror: updateTileInfo loadend: detect
} }
}), }),
new OpenLayers.Layer.WMS("OSGeo (same origin - proxied)", "http://vmap0.tiles.osgeo.org/wms/vmap0", { new OpenLayers.Layer.WMS("OSGeo (same origin - proxied)", "http://vmap0.tiles.osgeo.org/wms/vmap0", {
layers: "basic" layers: "basic"
}, { }, {
eventListeners: { eventListeners: {
tileloadstart: function(evt) { tileloadstart: function(evt) {
// send requests through proxy // send requests through proxy
evt.tile.url = "proxy.cgi?url=" + encodeURIComponent(evt.tile.url); evt.tile.url = "proxy.cgi?url=" + encodeURIComponent(evt.tile.url);
}, },
loadend: updateLayerInfo, tileloaded: updateStatus
tileloaded: updateTileInfo }
} })
})
], ],
center: [0,0], center: [0,0],
zoom: 1 zoom: 1
@@ -34,7 +33,6 @@ function init(){
stopSeeding(); stopSeeding();
} }
status.innerHTML = "Cache full."; status.innerHTML = "Cache full.";
cacheFull = true;
} }
} }
}); });
@@ -67,9 +65,7 @@ function init(){
hits = document.getElementById("hits"), hits = document.getElementById("hits"),
previousCount = -1, previousCount = -1,
cacheHits = 0, cacheHits = 0,
cacheFull = false,
seeding = false; seeding = false;
updateLayerInfo();
var read = document.getElementById("read"); var read = document.getElementById("read");
read.checked = true; read.checked = true;
read.onclick = toggleRead; read.onclick = toggleRead;
@@ -82,36 +78,34 @@ function init(){
tileloadstart.onclick = setType; tileloadstart.onclick = setType;
document.getElementById("tileerror").onclick = setType; document.getElementById("tileerror").onclick = setType;
document.getElementById("seed").onclick = startSeeding; document.getElementById("seed").onclick = startSeeding;
// update the number of cached tiles and detect local storage support // detect what the browser supports
function updateLayerInfo(evt) { function detect(evt) {
if (window.localStorage) { // detection is only done once, so we remove the listener.
if (previousCount !== localStorage.length) { evt.object.events.unregister("loadend", null, detect);
status.innerHTML = localStorage.length + " entries in cache."; var tile = map.baseLayer.grid[0][0];
try {
var canvasContext = tile.getCanvasContext();
if (canvasContext) {
// will throw an exception if CORS image requests are not supported
canvasContext.canvas.toDataURL();
} else {
status.innerHTML = "Canvas not supported. Try a different browser.";
} }
previousCount = localStorage.length; } catch(e) {
} else { // we remove the OSM layer if CORS image requests are not supported.
status.innerHTML = "Local storage not supported. Try a different browser."; map.setBaseLayer(map.layers[1]);
evt.object.destroy();
layerSwitcher.destroy();
} }
} }
// update the number of cache hits and detect missing CORS support // update the number of cache hits and detect missing CORS support
function updateTileInfo(evt) { function updateStatus(evt) {
if (cacheWrite.active) { if (window.localStorage) {
try { status.innerHTML = localStorage.length + " entries in cache.";
var canvasContext = evt.tile.getCanvasContext(); } else {
if (canvasContext) { status.innerHTML = "Local storage not supported. Try a different browser.";
// will throw an exception if CORS image requests are not supported
canvasContext.canvas.toDataURL();
} else {
status.innerHTML = "Canvas not supported. Try a different browser.";
}
} catch(e) {
if (seeding) {
stopSeeding();
}
status.innerHTML = "CORS image requests not supported. Try a different layer.";
}
} }
if (evt.tile.url.substr(0, 5) === "data:") { if (evt.tile.url.substr(0, 5) === "data:") {
cacheHits++; cacheHits++;
@@ -137,8 +131,7 @@ function init(){
// clear all tiles from the cache // clear all tiles from the cache
function clearCache() { function clearCache() {
OpenLayers.Control.CacheWrite.clearCache(); OpenLayers.Control.CacheWrite.clearCache();
cacheFull = false; updateStatus();
updateLayerInfo();
} }
// activate the cacheRead control that matches the desired fetch strategy // activate the cacheRead control that matches the desired fetch strategy
@@ -185,7 +178,7 @@ function init(){
// adjust the layer's buffer size so we don't have to pan // adjust the layer's buffer size so we don't have to pan
layer.buffer = Math.ceil((extentWidth / tileWidth - map.getSize().w / tileWidth) / 2); layer.buffer = Math.ceil((extentWidth / tileWidth - map.getSize().w / tileWidth) / 2);
map.zoomIn(); map.zoomIn();
if (cacheFull || nextZoom === layer.numZoomLevels-1) { if (nextZoom === layer.numZoomLevels-1) {
stopSeeding(); stopSeeding();
} }
} }