简体   繁体   English

使用OpenLayers判断地图图块图像是否已成功加载的方法

[英]Way to tell if a map tile image has loaded successfully using OpenLayers

Using OpenLayers, is there an easy way to tell if a map image has loaded correctly, perhaps using the “loadend” event? 使用OpenLayers,是否可以通过“loadend”事件轻松判断地图图像是否已正确加载?

We are reading map tiles from GeoServer, and using OpenLayers to display them. 我们正在从GeoServer中读取地图图块,并使用OpenLayers来显示它们。 Most of the time, the image displays in a reasonable time. 大多数情况下,图像会在合理的时间内显示。 Occasionally (especially when scrolling back and forth using the mouse wheel), the “loadend” event is fired as if the image was loaded, but nothing is displayed. 偶尔(特别是使用鼠标滚轮来回滚动时),“loadend”事件会被激活,就好像图像已加载一样,但没有显示任何内容。

When I investigate the HTML, it seems that the image has the correct URL, but has a background colour of pink (usually it is transparent), and I can't see anything useful in the event object. 当我调查HTML时,似乎图像具有正确的URL,但背景颜色为粉红色(通常是透明的),我在事件对象中看不到任何有用的东西。

Any ideas? 有任何想法吗?

It is likely that GeoServer is simply failing to generate the tiles from time to time. 很可能GeoServer不时无法生成切片。 There may be some tweaking you can do to improve the performance, so I might invest some time looking into that. 你可以做些调整来提高性能,所以我可能会花一些时间来研究它。

From an OpenLayers perspective, there are a few simple things you can do that may help. 从OpenLayers的角度来看,您可以做一些可能有用的简单事情。 First, there is a property called OpenLayers.IMAGE_RELOAD_ATTEMPTS that tells the layer how many times to retry if it fails to get the image. 首先,有一个名为OpenLayers.IMAGE_RELOAD_ATTEMPTS的属性,它告诉图层如果无法获取图像,将重试多少次。 This value defaults to 0 (zero). 该值默认为0(零)。 I would try setting it to 3 at the top of your code and see if that helps. 我会尝试将其设置为代码顶部的3,看看是否有帮助。

OpenLayers.IMAGE_RELOAD_ATTEMPTS = 3;

Also, if you get a broken tile and want it to show up as something other than pink, you can update the OpenLayers.Util.onImageLoadErrorColor value at the top of your code. 此外,如果你得到一个破碎的瓷砖并希望它显示为粉红色以外的东西,你可以更新代码顶部的OpenLayers.Util.onImageLoadErrorColor值。

OpenLayers.Util.onImageLoadErrorColor = 'transparent';

You can browse the source for all of this here: http://trac.openlayers.org/browser/trunk/openlayers/lib/OpenLayers/Util.js 您可以在此处浏览所有这些内容的来源: http//trac.openlayers.org/browser/trunk/openlayers/lib/OpenLayers/Util.js

with OpenLayers 2.12 the css needed is 使用OpenLayers 2.12所需的css是

    .olImageLoadError
    {
        display: none !important;
    }

If there was an error it should have the following CSS class in the HTML: olImageLoadError 如果出现错误,则HTML中应包含以下CSS类: olImageLoadError

if you want to hide it add the following to your stylesheet: 如果要隐藏它,请将以下内容添加到样式表中:

.olImageLoadError { 
    display: none;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM