如何根据互联网连接可用性切换基础层 [英] How to switch base layer based on internet connection availability

查看:85
本文介绍了如何根据互联网连接可用性切换基础层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序(html + JavaScript)使用Google地图作为基础图层。我还将OSM设置为脱机基础层,以便在没有互联网连接时使用。

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.

现在,我锁定了一种为应用程序检查Internet连接的方法,如果有,然后使用谷歌地图作为基础层。如果不是,则使用OSM。

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.

推荐答案

当加载Google时,您可以侦听'loaderror'事件地图图块失败。以下是常规WMS图层的示例(完整示例,位于 http://jsfiddle.net/D3Eha/2/

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;
                    }
                }
            }
        }        
    }
);

当计算机再次联机时,检测是非常棘手的。例如,当你离线时,启动定期计时器从Internet加载一些图像。如果成功,您将重新上线。

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.

这篇关于如何根据互联网连接可用性切换基础层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆