LeafletJS检查没有加载地图 [英] LeafletJS check for not loading map

查看:215
本文介绍了LeafletJS检查没有加载地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测地图是否正确加载?
发生的事情是,在我的离子应用程序中,地图网址停止工作,地图没有出错。它只显示灰色。

How can I detect whether a map is loading properly or not? What happened was that in my ionic app the map url stopped to work, and the map did not throw an error. It only displayed gray.

有没有办法正确检查这个事件并回到另一个地图?

Is there a way to check for this event properly and fall back to an alternative map?

推荐答案

一个通常的但简单的解决方法是指定单个回退瓷砖,使用 errorTileUrl选项。通常它会显示一个浅红色十字,让用户知道地图服务器没有任何图像可以提供该区域和缩放。

A usual but simplistic workaround is to specify a single fallback tile, using the errorTileUrl option of your tileLayer. Typically it shows a light red cross to let the user know the map server does not have any image to provide for that area and that zoom.

现在,如果你想成为更智能和回退到不同的地图(即重定向到不同的URL),我不知道任何开箱即用的解决方案,但它是非常好的可行。我们的想法是覆盖 L.TileLayer 类的 _tileOnError 方法,该方法通常用以下方法替换 errorTileUrl 收到404错误(即服务器上没有可用的磁贴)。在给定的链接中,它会自动将URL替换为不同位置的新URL(可能是不同的服务器),因为它只需要丢失几个磁贴。

Now if you want to be smarter and fallback to a different map (i.e. redirect to a different URL), I am not aware of any out-of-the box solution, but it is very well do-able. The idea is to override the _tileOnError method of the L.TileLayer class, which normally replaces the tile by the one from errorTileUrl when it receives a 404 error (i.e. no tile available on the server). In the given link, it automatically replaces the URL by a new one for a different location (could be a different server), as it expects only a few tiles to be missing.

现在,如果您假设整个服务器将停止响应并且您想要切换到另一个tileLayer(以便它停止ping错误的服务器),您可以实现一个简单的计数器,其增量为 _tileOnError 方法,一旦达到给定的阈值,就切换图层。

Now if you assume the whole server would stop responding and you want to switch over to a different tileLayer (so that it stops pinging the faulty server), you could implement for example a simple counter that is incremented by the _tileOnError method, and once a given threshold is reached, switch the layers.

这篇关于LeafletJS检查没有加载地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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