[OpenLayers-Dev] Advanced styles with OpenLayers

Andreas Hocevar ahocevar at opengeo.org
Fri Mar 6 12:00:11 EST 2009


Hi,

2009/3/3 Gorka López Rivacoba <gorka.lopez.rivacoba at gmail.com>:
> I have a GML layer and I want to asign differents colours to its polygons
> (default style). I want to assign a style when the polygons are selected too
> (the same style to all the polygons in the layer).
>
> I try to assign a different stylemap to each layer's polygon (not depends on
> the features attributes) like in http://trac.openlayers.org/wiki/Styles, but
> it seems that OpenLayers.Feature.Vector doesn´t support to asign a mapstyle
> (in order than a basic style):
>
>
> if
>
> (...)style = {fillColor: "#FCED97", strokeColor: "#FCE144", fillOpacity:
> 0.5, strokeWidth: 2};
>
> else if (...)style = {fillColor: "#000000", fillOpacity: 0.1, strokeColor:
> "#000000", strokeWidth: 1};

The if/else you are doing here is exactly what a StyleMap is used for.
See the http://www.openlayers.org/dev/examples/styles-context.html
example, especially the 2nd StyleMap with the functions in the context
to see how to do that.

> var styleSel = {fillColor: "#FC1797", strokeColor: "#FC1797", fillOpacity:
> 0.5, strokeWidth: 2};
>
> var myStyleMap = new OpenLayers.StyleMap({"default": style,"select":
> styleSel});
>
> var vector = new OpenLayers.Feature.Vector(geometry, record, myStyleMap);

You cannot assign a StyleMap to the style argument of Feature.Vector.
The third argument of Feature.Vector needs to be a plain symbolizer
object (i.e. with just the fillColor, strokeColor or whatever
properties) or a n OpenLayers.Style object, but not an
OpenLayers.StyleMap.

>
> layer.addFeatures(vector);
>
>
>
> I try to assign a default style to each polygon and assign the selected
> style to the OpenLayers.Layer.GML (it's always the same style), but in this
> case the selected style is not shown:
>
>
> var styleSel = {fillColor: "#FF7474", strokeColor: "#FF0000", fillOpacity:
> 0.5, strokeWidth: 2};
>
> var myStyleMap = new OpenLayers.StyleMap({"select": styleSel});
>
> layerEditable = new OpenLayers.Layer.GML("Polygons", urlGML,{styleMap:
> myStyleMap});
>
> if (...)style = {fillColor: "#FCED97", strokeColor: "#FCE144", fillOpacity:
> 0.5, strokeWidth: 2};
>
> else if (...)style = {fillColor: "#000000", fillOpacity: 0.1, strokeColor:
> "#000000", strokeWidth: 1};
>
> var vector = new OpenLayers.Feature.Vector(geometry, record, style);

This combination won't work. Either use a styleMap for both default
and select styles (recommended), or use the style property of each
feature for the default style plus the selectStyle property of
OpenLayers.Control.SelectFeature for the select style.

> layer.addFeatures(vector);
>
>
>
> Is there a way to assign differents styles in each polygon of a layer and a
> selected style to the complete layer?

As described above, the select style needs to be put into the
selectStyle property of OpenLayers.Contorl.SelectFeature in this case.

Regards,
Andreas.


-- 
Andreas Hocevar
OpenGeo - http://opengeo.org/
Expert service straight from the developers.



More information about the Dev mailing list