[OpenLayers-Users] wms untiled 'busy loading' message or image

Benoit Myard myardbenoit at gmail.com
Tue Jul 10 09:58:46 EDT 2007


// Here is the code:

OpenLayers.Control.LoadMonitor = OpenLayers.Class.create();
OpenLayers.Control.LoadMonitor.prototype =
  OpenLayers.Class.inherit( OpenLayers.Control, {

    div: null,
    counter: 0,

    activate: function() {
      var img = OpenLayers.Util.getImagesLocation() + "loading.gif";
      var size = new OpenLayers.Size(40, 40);
      var dim = OpenLayers.Element.getDimensions(this.map.viewPortDiv);
      var pos = new OpenLayers.Pixel((dim.width - size.w) / 2,
                                    (dim.height - size.h) / 2);

      this.div = OpenLayers.Util.createDiv("LoadMonitor", pos, size, img);
      this.div.style.zIndex = 1000;

      this.map.viewPortDiv.appendChild(this.div);
    },

    deactivate: function() {
      if (this.div) {
        this.div.style.display = "none";
        this.div = null;
      }
    },

    draw: function() {
      this.activate();
    },

    monitor: function(layer) {
      layer.events.register('loadstart', this, this.start);
      layer.events.register('loadcancel', this, this.end);
      layer.events.register('loadend', this, this.end);
    },

    start: function(layer) {
      this.counter++
      if (this.counter > 0 && this.div)
        this.div.style.display = "block";
    },

    end: function(layer) {
      if (this.counter > 0)
          this.counter--;
      if (this.counter == 0 && this.div)
        this.div.style.display = "none";
    },

    CLASS_NAME: "OpenLayers.Control.LoadMonitor"
});

// And here is an example:

var map = new OpenLayers.Map(...);

var monitor = new ExpWeb.Control.LoadMonitor();
map.addControl(monitor);

var layer = new OpenLayers.Layer.WMS(...);
monitor.monitor(layer);
map.addLayer(layer);

It's not automated because I had to have the ability to choose which
layer to monitor. Also, for some reason, events are somewhat strange.
Add breakpoints in start() and end() if you use FireBug and note
events triggered. Any idea ?

Cheers.

-- 
Benoit Myard



More information about the Users mailing list