[OpenLayers-Dev] Need newbie help on developing custom layer

Tim Schaub tschaub at opengeo.org
Wed Feb 3 13:00:04 EST 2010

Vincent Gulinao wrote:
> I'm a total newbie on Openlayers, and map dev in general, so I'm a little
> lost how to start.
> I need to develop custom layer defining map political boundaries just like
> this: http://www.google.com/intl/en_us/2008election/.
> I'd appreciate pointers, code sample and docs links, learning path
> suggestions, etc. that you may share.

You need a number of things to accomplish this.

You'll need spatial data for your political boundaries.  This will 
include whatever attribute data you want to display to the user 
(election results per state or whatever).

If you want to render the data in the browser and have the user interact 
with features (states), you'll need to set up something to serve up the 
vector data.  If your data never changes, you could store your data on 
the filesystem in some format like GeoJSON and have Apache serve it up 
(e.g. http://example.com/data/states.json).

If you go this route, you could set up a map in OpenLayers using a 
vector layer (OpenLayers.Layer.Vector) with a fixed strategy 
(OpenLayers.Srategy.Fixed) and the HTTP protocol 
(OpenLayers.Protocol.HTTP) using the GeoJSON format 

Somewhat relevant example (using GML format instead of GeoJSON):

Alternatively, if you want to allow users to query your data based on 
location or attribute (e.g. get all states in this box, get all states 
that voted for G.W.), you will want to set up a service that can 
dynamically serve up vector data based on user queries.  A Web Feature 
Service (WFS) is designed to do just this.  GeoServer and MapServer are 
examples of a WFS.

If you go this route, you could set up a map in OpenLayers using a 
vector layer with a fixed or bounding box strategy 
(OpenLayers.Strategy.BBOX) and the WFS protocol (OpenLayers.Protocol.WFS).

somewhat relevant example:

Both of the above approaches request vector data from your server and 
render the results in the browser.  This has the advantage of letting 
you dynamically style the data on the client and lets your users 
interact with the data attributes (with less latency).

See the style examples (http://openlayers.org/dev/examples/?q=style) to 
learn about applying custom styles client-side.

See the feature selection & popup example to learn how to display 
feature attributes in a popup (assuming you have feature data client 
side): http://openlayers.org/dev/examples/select-feature-openpopup.html

As an alternative to rendering the data client side (as outlined above), 
you can set up a service to render your data on the server.  The Web Map 
Service (WMS) is designed to provide feature rendering on the server. 
GeoServer and MapServer are WMS implementations.

If you go this route (render data on the server), you configure your 
OpenLayers map with a WMS layer (OpenLayers.Layer.WMS).  Many of the 
OpenLayers examples use a WMS layer (e.g. 

If your server is responsible for rendering feature data (political 
boundaries) instead of the browser, to allow your users to view feature 
attributes (who voted for G.W.) you need to configure your OpenLayers 
map to issue request for feature information.

The WMS protocol defines a GetFeatureInfo method for this purpose. 
Here's an example that issues GetFeatureInfo requests and displays the 
result in a popup: 

The drawback of requesting all your vector data client side is that you 
can swamp the browser while trying to parse or render the features.  You 
need to use appropriate strategies to limit the quantity of data that is 
parsed and rendered.  The drawback of leaving all your vector data on 
the server is that there is more latency in displaying feature 
attributes to the user (user waits while requests are issued to the server).

None of this requires that you develop a custom OpenLayers layer type. 
It does require that you set up the appropriate services on the back-end.

Hope this helps get you started.

> TIA.
> ------------------------------------------------------------------------
> _______________________________________________
> Dev mailing list
> Dev at openlayers.org
> http://openlayers.org/mailman/listinfo/dev

Tim Schaub
OpenGeo - http://opengeo.org
Expert service straight from the developers.

More information about the Dev mailing list