[OpenLayers-Users] Toggling layers without LayerSwitcher

Kimball, David (DCR) David.Kimball at state.ma.us
Wed Oct 1 11:14:28 EDT 2008


Hi everyone,

I'm working on my first OpenLayers map and I'm having a problem with
toggling layer visibility.  I want to use an HTML form with radiobuttons
to toggle the layers instead of the LayerSwitcher control.  When I
choose a layer other than the first layer in the list, it draws fine
once but if you do any zooming or panning it will not redraw.  If you
choose the first layer, it works fine.  Here's the map:

http://maps.massgis.state.ma.us/dcr/forestry09.html

The function that chooses the new layer is called changeSymbolization
(it's called by the radiobuttons' change event).  It loops through the
layers and makes all but one invisible.


	function changeSymbolization(byField){
		var byFieldSplit = byField.toString().split(',');
		lz = map.layers;
		for (n=0; n < lz.length; n++) {
			lz[n].setVisibility(lz[n].name ==
byFieldSplit[0]);
		}
		document.getElementById("theLegendTitle").innerHTML =
byFieldSplit[1];
		document.getElementById("theLegend").src =
"http://giswebservices.massgis.state.ma.us/geoserver/wms?VERSION=1.1.0&R
EQUEST=GetLegendGraphic&LAYER=massgis:DCR.TOWNS_POLYM_FORESTRY&STYLE=DCR
.TOWNS_POLYM_FORESTRY::" + byFieldSplit[0] +
"&WIDTH=16&HEIGHT=16&FORMAT=image/png";

	}


[this function is called by radiobuttons, for example]:


<input type="radio" name="chosenSymbology"
onclick="changeSymbolization(this.value)" value="FSCARS_SC,Towns
symbolized by<br/>National Forest Service<br/>CARS Score:" /> Federal
CARS score<br />


After trying a few approaches, I set it up so there are 17 different
layers added to the map, each of which contains all the background
layers plus the foreground layer (which is symbolized by a different
attribute in each layer).  This is done in the init() function.  I'm not
sure this is the best way to do it, but when I tried having the
background and foreground layers in separate layers, the foreground
layer would block the background layers by drawing a white background on
the whole map before drawing the foreground layer.

[Here is what each layer looks like (there are 17 of these, each with a
different name and different Style for one of the layers)]:


IMPERV_PCT_Layer = new OpenLayers.Layer.WMS(
      "IMPERV_PCT",
"http://giswebservices.massgis.state.ma.us/geoserver/wms",
      {
          width: '600',
          srs: 'EPSG:26986',
          layers:
'massgis:DCR.TOWNS_POLYM_FORESTRY,massgis:GISDATA.OCEANMASK_POLY,massgis
:GISDATA.NEMASK_POLY,massgis:DCR.TOWNS_POLYM_FORESTRY,massgis:GISDATA.TO
WNS_POLY',
          height: '358',
          styles:
',,GISDATA.NEMASK_POLY::Light_Yellow,DCR.TOWNS_POLYM_FORESTRY::IMPERV_PC
T,GISDATA.TOWNS_POLY::Labels_Med',
          format: 'image/png'
      },
      {singleTile: true, ratio: 1} 
);
IMPERV_PCT_Layer.setVisibility(0);


[Here is how they're added to the map]:


map.addLayers([SUSTCOM_SC_Layer,FSCARS_SC_Layer,ST_ASST_YN_Layer,MGTPLAN
_YN_Layer,ADV_GRP_YN_Layer,LOC_ORD_YN_Layer,PROSTAF_YN_Layer,TREEINV_YN_
Layer,TREECITYYN_Layer,OSPLAN_YN_Layer,PDENSQMI00_Layer,BUDGET_P_C_Layer
,UFORE_YN_Layer,STRATUM_YN_Layer,UCF_INDEX_Layer,CANOPY_COV_Layer,IMPERV
_PCT_Layer]);


I tried to find a similar example somewhere, but they seem to use the
layerswitcher control and be really simple (Examples) or be so complex
that I couldn't make heads of tails of them (Gallery).

Does anyone have a suggestion for how to fix the redrawing problem, or a
better approach to dealing with the layer switching that would solve the
problem?  Thank you for your help!

--David



More information about the Users mailing list