Trouble with OpenLayers Styles.

Posted by Jenny on Stack Overflow See other posts from Stack Overflow or by Jenny
Published on 2010-04-30T14:38:16Z Indexed on 2010/04/30 15:57 UTC
Read the original article Hit count: 573

Filed under:
|

So, tired of always seeing the bright orange default regular polygons, I'm trying to learn to style OpenLayers.

I've had some success with:

     var layer_style = OpenLayers.Util.extend({},OpenLayers.Feature.Vector.style['default']);
  layer_style.fillColor = "#000000";
 layer_style.strokeColor = "#000000";
 polygonLayer = new OpenLayers.Layer.Vector("PolygonLayer");
 polygonLayer.style = layer_style;

But sine I am drawing my polygons with DrawFeature, my style only takes effect once I've finished drawing, and seeing it snap from bright orange to grey is sort of disconcerting. So, I learned about temporary styles, and tried:

 var layer_style =  new OpenLayers.Style({"default": {fillColor: "#000000"}, "temporary": {fillColor: "#000000"}})
polygonLayer = new OpenLayers.Layer.Vector("PolygonLayer");
polygonLayer.style = layer_style;

This got me a still orange square--until I stopped drawing, when it snapped into completely opaque black. I figured maybe I had to explicitly set the fillOpacity...no dice. Even when I changed both fill colors to be pink and blue, respectively, I still saw only orange and opaque black.

I've tried messing with StyleMaps, since I read that if you only add one style to a style map, it uses the default one for everything, including the temporary style.

var layer_style = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']);
var style_map = new OpenLayers.StyleMap(layer_style);
polygonLayer = new OpenLayers.Layer.Vector("PolygonLayer");
polygonLayer.style = style_map;

That got me the black opaque square, too. (Even though that layer style works when not given to a map). Passing the map to the layer itself like so:

polygonLayer = new OpenLayers.Layer.Vector("PolygonLayer", style_map);

Didn't get me anything at all. Orange all the way, even after drawn.

polygonLayer = new OpenLayers.Layer.Vector("PolygonLayer", {styleMap: style_map});

Is a lot more succesful: Orange while drawing, translucent black with black outline when drawn. Just like when I didn't use a map. Problem is, still no temporary...

So, I tried initializing my map this way:

var style_map = new OpenLayers.StyleMap({"default": layer_style, "temporary": layer_style});

No opaque square, but no dice for the temporary, either... Still orange snapping to black transparent. Even if I make a new Style (layer_style2), and set temporary to that, still no luck. And no luck with setting "select" style, either.

What am I doing wrong? Temporary IS for styling things that are currently being sketched, correct? Is there some other way specific to the drawFeature Controller?

Edit: setting extendDefault to be true doesn't seem to help, either...

var style_map = new OpenLayers.StyleMap({"default": layer_style, "temporary": layer_style}, {"extendDefault": "true"});

© Stack Overflow or respective owner

Related posts about openlayers

Related posts about style