[OpenLayers-Users] FW: Toggling layers without LayerSwitcher
Kimball, David (DCR)
David.Kimball at state.ma.us
Tue Oct 7 10:00:46 EDT 2008
Hi everyone, I looked into this a bit more and I have a little more info
on what's happening (although I still haven't solved the problem).
If I create my map with both the layerswitcher control and my HTML
radiobuttons:
http://maps.massgis.state.ma.us/dcr/forestry11.html
it becomes clear that the HTML radiobuttons are making layers
visible/invisible, but the layerswitcher control is not updating to
match the chosen layer.
If I look at the requests being sent to the server using FireBug, it
becomes clear that clicking an HTML radiobutton other than the
default/top choice causes the correct request to be sent (i.e. the
correct Layer with the correct STYLES is requested and the appropriate
image is returned and displayed). Then, when a pan or zoom operation is
performed on the map, an incorrect request is sent, with the
top/original layer (with STYLE of DCR.TOWNS_POLYM_FORESTRY::SUSTCOM_SC
for the main layer) being requested (probably because this is still the
selected layer in the layerswitcher control?). An image is returned
from the server showing the top/original layer instead of the user's
selected layer, and the map does not get refreshed (probably because the
top layer, in the returned image, is set to invisible).
A few clarifications:
1) I want to have radiobuttons that are outside the map so that the user
can't miss seeing them. I've had trouble getting the layerswitcher to
appear where and how I want it when placing it outside the map, and I
think the standard layerswitcher tool inside the map is too "hidden" for
the likely users of this map.
2) Clicking a radiobutton needs to change the style that the towns layer
uses to symbolize itself, update the legend graphic, and add a title
above the legend. I'm not sure if the layerswitcher can trigger the
latter two.
3) The way I've implemented this is to have one Layer in the map for
each radiobutton. Clicking on a radiobutton makes all layers but that
one invisible. I'm open to different approaches if anyone can think of
one.
4) What's going wrong is that the radiobuttons don't properly change the
layer. Clicking on them works at first, but then if you pan or zoom
there is no redraw, because subsequent server requests are asking for
the top/original layer, not the user-selected layer.
I would greatly appreciate any reply!
Thanks!
--David
-----Original Message-----
Subject: Toggling layers without LayerSwitcher
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