[OpenLayers-Users] updating layer name in LayerSwitcher

Steven Chall stevec at renci.org
Thu Dec 6 09:07:32 PST 2012


I set up a map and a layer switcher thusly:


var map = new OpenLayers.Map("liveMap", options);
map.addLayer(new OpenLayers.Layer.OSM());
var layerSwitcher = new OpenLayers.Control.LayerSwitcher();
map.addControl(layerSwitcher);

Then I add a data layer:

...

  var points = new OpenLayers.Layer.Vector(
      variableName,
      {
        styleMap: dataStyles,
        rendererOptions: {zIndexing: true}
      }
  );

// dataPoints is an Array of new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(etc etc))...

  points.addFeatures(dataPoints);
  map.addLayer(points);
...// and other stuff to populate the layer...

That all works fine.  But I need to periodically let the user change some attributes of the layer, and there needs to be a name change to the layer at that time.  I change the name of the layer like this::

map.layers[indexOfDatalayer].name = newName;

and the name shows up in the layer as I look at the data structure's values using Firebug's Watch functionality.  However, I can't get the name of the layer that shows by its checkbox in layerSwitcher to reflect the name change (except sporadically and by doing other undesirable things to the layerSwitcher). I've even gone so far as to destroy the layerSwitcher and create a new one, thinking it would pick up on the new layer name, but it doesn't:

layerSwitcher.destroy;
layerSwitcher = new OpenLayers.Control.LayerSwitcher();
map.addControl(ShipDataSet.layerSwitcher);

Some other things I've tried:
map.controls[4].layerStates[1].name = name; // the layerSwitcher is map.controls[4].
map.controls[4].loadContents();
map.controls[4].updateMap();
map.controls[4].redraw();
layer.refresh();

I'd really appreciate any suggestions on getting the name of the layer to change in the layerSwitcher when I change the name of the layer.  Thanks.

Steve Chall


More information about the Users mailing list