Printer Friendly
In this tutorial we will just show some example snippets of using OpenLayers that we have found most useful.
We will be assuming Open Layers 2.4 and above.
Initializing Layers Off
In our previous example, you will notice that all the layers we have added are turned on by default. In order to turn the layers off, we can pass in the additional parameter of visibility. Like the below line 28. Note that visibility applies to all classes of layers, not just WMS layers.
26: wmstaxi = new OpenLayers.Layer.WMS("MASSGIS Boston Taxi Stops", "http://giswebservices.massgis.state.ma.us/geoserver/wms",
27: {layers: "massgis:GISDATA.WATERTAXISTOPS_PT", transparent: "true", format: "image/gif"},
28: {tileSize: new OpenLayers.Size(400,400), buffer: 1, visibility: false })
The above effect can be seen http://www.bostongis.com/demos/OpenLayers/example2.htm.
If you want to turn off a layer after initialization, then you would use the setVisibility method of a layer. For example - mylayer.setVisibility(false). This would be useful say if you did not want to use the built-in layer switcher, but instead for example have sets of layers display and not display based on others.
Forcing a layer to be a Base Layer
Within an OpenLayers map, you can have only one layer selected as Base.
This layer will control the projection of other layers among other things.
In the layer switcher, you will see base layer options marked at the top in a category section called Base Layer.
Each option will have a radio button instead of a checkbox to prevent from selecting more than one.
By default the first Base Layer added to the map becomes the active base layer.
Certain layers have to be base layers because they are not transparent and there are other things you can't control about them like projection and so forth.
For example commercial layers such as Google, Yahoo, and Virtual Earth are just assumed to be base layers so you don't have to explicitly state them to be and actually can't force them not to be.
If you have a WMS layer for example, it can be used as a base layer or an overlay layer. If you don't explicitly state it is a base layer,
it will be assumed to be an overlay layer. So how do you force baseness you ask, simple by setting the isBaseLayer property of the layer as shown below.
map.addLayer(new OpenLayers.Layer.WMS.Untiled("MassGIS Boundary", "http://giswebservices.massgis.state.ma.us/geoserver/wms",
{'layers': "massgis:GISDATA.BOUNDARY_ARC",'transparent':"true",'format': "image/png"}, {'isBaseLayer':true}));
To Tile or Not to Tile
There are currently two kinds of layers in which you have the option of tiling and not tiling.
These are WMS layers and Mapserver layers. Each of these are exposed as separate layer classes WMS, WMS.Untiled, Mapserver, Mapserver.Untiled.
In what circumstances would you want to tile and other circumstances where you would not want to tile. To first answer this question, its useful to think about what happens when you tile.
When maps are tiled, the map viewport is broken out into several quandrants - which tries to approximate what you have set for TileSize, but often falls short. So for each load of the screen, multiple requests sometimes in the order of 60 per layer paint is requested.
You can imagine this can be a considerable strain on a weak server and especially silly when what you are asking for is a light boundary file of say Boston. In the untiled case, only one request is made per layer, but the request is usually on the order of 4 times larger
than the view port to allow for smoother panning effects at the cost of a little slower load time.
Outlined below are the cases where we've found tiling or untiling useful. Some are a matter of taste and some are a matter of practicality.
Use Untiled in the following scenarios
- Fairly light-weight (in file size) that span huge areas
- Processor constrained WMS server
- If you find it annoying that half of your map paints while the other half is loading
- High band-width server and high band-width clients
- Your map images return embedded scales.
Use tiled during the following scenarios
- Fairly heavy geometries (in file size) that span small areas
- Super fast WMS server or server with tile caching built in
- Low band-width clients
- Relatively long pauses of a completely blank map area that suddenly loads all at once annoys you.
An example of an untiled layer is shown in our visibility discussion. An example of an untiled is shown in our Base Layer discussion.
Post Comments About Using OpenLayers: Part 2