diff --git a/doc-src/olwidget.js.rst b/doc-src/olwidget.js.rst index 4a5ff97..c98decb 100644 --- a/doc-src/olwidget.js.rst +++ b/doc-src/olwidget.js.rst @@ -306,7 +306,31 @@ General map display ``'ve.shaded'``, ``'ve.aerial'``, ``'ve.hybrid'``, ``'wms.map'``, ``'wms.nasa'``, ``'yahoo.map'``, and ``'cloudmade.'`` (where ```` is the number for a cloudmade style). A blank map can be obtained using - ``'wms.blank'``. Additional providers or options can be manually added + ``'wms.blank'``. + + Other providers can be added by choosing ``custom.`` where + ```` corresponds to a property in a ``customBaseLayers`` + object that you would need to create. It must have a ``class`` + property corresponding to an OpenLayers layer subclass, and an + ``args`` property that is a list of arguments to pass to that + constructor. For example:: + + var customBaseLayers = { + 'opengeo_osm': # to use this, your olwidget layers would include ['custom.opengeo_osm'] + {"class": "WMS", # The OpenLayers.Layer subclass to use. + "args": [ # These are passed as arguments to the constructor. + "OpenStreetMap (OpenGeo)", + "http://maps.opengeo.org/geowebcache/service/wms", + {"layers": "openstreetmap", + "format": "image/png", + "bgcolor": "#A1BDC4", + }, + {"wrapDateLine": True + }, + ], + }} + + Additional options and layers can also be manually added using the normal OpenLayers apis (see `this provider example `_). diff --git a/js/olwidget.js b/js/olwidget.js index cd200e5..3dec235 100644 --- a/js/olwidget.js +++ b/js/olwidget.js @@ -144,6 +144,32 @@ var olwidget = { }); } }, + custom: { + // Support for arbitrary OpenLayers base layers. + // To use this, ensure that customBaseLayers[type] exists, and + // has both a 'class' string (name of the OL constructor) and + // an 'args' array to pass to that constructor. + map: function(type) { + var classname = customBaseLayers[type]['class']; + var class_ = OpenLayers.Layer[classname]; + var args = customBaseLayers[type].args; + // Can't use .apply() directly with an OL constructor, + // because we don't have a suitable `this` argument. + // Instead make a constructor function with a .prototype + // property the same as our class.prototype. + // The `new` keyword creates an instance from that prototype + // which will be used as `this` in the constructor call. + // `new` is also the only way to get the new instance to have + // the correct actual prototype, which is *not* the same as + // class.prototype. Thanks Tim Schaub for explaining this bit of + // javascript OOP to me. + var constructor = function() { + class_.prototype.initialize.apply(this, args); + }; + constructor.prototype = class_.prototype; + return new constructor(); + } + }, /* * Utilities */