[OpenLayers-Users] optimizations to deliver openlayers js faster?

Christopher Schmidt crschmidt at metacarta.com
Tue Jan 9 20:47:58 EST 2007


On Tue, Jan 09, 2007 at 06:38:36PM -0500, Chris Holmes wrote:
> Ah, so just over-ride on the client side or some such?  I still want to 
> use the zipped up single build file.
> 
> The two I'm using right now are multiple hostnames and the multiple 
> reload tries when a single tile won't come up.  I'm not in a huge rush 
> though, I can just do static linking from the home page.

Yep. If you look in:

http://labs.metacarta.com/osm/

you'll see, in the main page, code that looks like this:
-----
OpenLayers.Layer.WMS.prototype = OpenLayers.Class.inherit(
OpenLayers.Layer.WMS, { 
    getFullRequestString:function(newParams, altUrl) {
        var projection = this.map.getProjection();
    newParams.SRS = (projection == "none") ? null : projection;
        // use layer's url unless altUrl passed in
        var url = (altUrl == null) ? this.url : altUrl;

        // create a new params hashtable with all the layer params and
        // the 
        // new params together. then convert to string
        var allParams = OpenLayers.Util.extend(new Object(),
this.params);
        var allParams = OpenLayers.Util.extend(allParams, newParams);
        var paramsString =
OpenLayers.Util.getParameterString(allParams);
        if (typeof url == "object") {
            var serverid=0;
            for (var i=0; i<paramsString.length; i++) {
              serverid += paramsString.charCodeAt(i);
            }
            url = url[serverid%url.length];
        }

        // requestString always starts with url
        var requestString = url;

        if (paramsString != "") {
            var lastServerChar = url.charAt(url.length - 1);
            if ((lastServerChar == "&") || (lastServerChar == "?")) {
                requestString += paramsString;
            } else {
                if (url.indexOf('?') == -1) {
                    //serverPath has no ? -- add one
                    requestString += '?' + paramsString;
                } else {
                    //serverPath contains ?, so must already have
                    //paramsString at the end
                    requestString += '&' + paramsString;
                }
            }
        }
        return requestString;
    }
 
});
------

This will allow multi-server selection.

For the reload tries patch:

---
OpenLayers.IMAGE_RELOAD_ATTEMPTS = 3;
OpenLayers.Util.onImageLoadError = function() {
   this.style.backgroundColor = OpenLayers.Util.onImageLoadErrorColor;
   this._attempts = (this._attempts) ? (this._attempts + 1) : 1;
   if(this._attempts <= OpenLayers.IMAGE_RELOAD_ATTEMPTS) {
       this.src = this.src;
   } else {
       this.style.backgroundColor =
OpenLayers.Util.onImageLoadErrorColor;
   }
    this.style.display = "";
}; 
---


Just include these two chunks of code in  the javascript of the page
after the OpenLayers script is loaded, and before your map is loaded,
and you should be all set.

Regards,
-- 
Christopher Schmidt
MetaCarta



More information about the Users mailing list