Created
August 5, 2011 21:11
-
-
Save slinkp/1128539 to your computer and use it in GitHub Desktop.
Patch that adds support for arbitrary OpenLayers constructors to olwidget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/doc-src/olwidget.js.rst b/doc-src/olwidget.js.rst | |
index 4a5ff97..dd8504b 100644 | |
--- a/doc-src/olwidget.js.rst | |
+++ b/doc-src/olwidget.js.rst | |
@@ -306,7 +306,30 @@ General map display | |
``'ve.shaded'``, ``'ve.aerial'``, ``'ve.hybrid'``, ``'wms.map'``, | |
``'wms.nasa'``, ``'yahoo.map'``, and ``'cloudmade.<num>'`` (where ``<num>`` | |
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.<name>`` where | |
+ ``<name>`` 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 | |
<examples/other_providers.html>`_). | |
diff --git a/js/olwidget.js b/js/olwidget.js | |
index d530525..bd221a2 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 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment