views:

97

answers:

1

Using OpenLayers, is there an easy way to tell if a map image has loaded correctly, perhaps using the “loadend” event?

We are reading map tiles from GeoServer, and using OpenLayers to display them. 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.

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.

Any ideas?

+2  A: 

It is likely that GeoServer is simply failing to generate the tiles from time to time. 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. 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. This value defaults to 0 (zero). I would try setting it to 3 at the top of your code and see if that helps.

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 = 'transparent';

You can browse the source for all of this here: http://trac.openlayers.org/browser/trunk/openlayers/lib/OpenLayers/Util.js

atogle
Thank-you, very useful.IMAGE_RELOAD_ATTEMPTS was already set to 5.I noticed the “_attempts” attribute on the image that was returned. I assume that the underscore is meant to indicate it as a logically private attribute.This attribute is only defined when an actual reload attempt has been made, and in the case of a fail, its image was 6. By checking that the “_attempts” value was greater then IMAGE_RELOAD_ATTEMPTS I was able to spot when an image failed to load.
zod