Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to switch base layer based on internet connection availability

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.

like image 375
Noon Avatar asked May 20 '26 22:05

Noon


1 Answers

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.

like image 112
user1702401 Avatar answered May 23 '26 10:05

user1702401



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!