My application ( html+ JavaScript) use Google map as a base layer. I also setup OSM as an offline base layer to use when there is no internet connection.
Now I'm locking for a way to make app check for internet connection, if there is, then use Google map as a base layer. if not, then use OSM.
You can listen for 'loaderror' event, that will be thrown, when loading of Google Map tile fails. Here is example with regular WMS layer (full example in http://jsfiddle.net/D3Eha/2/)
var wmsLayer, workingOffline = false;
wmsLayer = new OpenLayers.Layer.WMS("OpenLayers WMS",
"http://vmap0.tiles.osgeo.org/wms/vmap0?",
{layers: 'basic'},
{
attribution: 'Provided by OSGeo',
tileOptions: {
eventListeners: {
'loaderror': function(evt) {
if (workingOffline == false) {
console.log('Tile load error, switching to offline map');
workingOffline = true;
}
}
}
}
}
);
It's trickier to detect, when computer is online again. For example, when you go offline, start periodical timer to load some image from Internet. If it succeeds, you are back online.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With