[OpenLayers-Users] need a clarification re styleMaps

Michael Rubinsky mike at theupstairsroom.com
Wed Oct 5 09:23:33 EDT 2011


Quoting openlayers-users-request at lists.osgeo.org:

>
>
> Today's Topics:
>
>    1. need a clarification re styleMaps (Puneet Kishor)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Tue, 4 Oct 2011 10:42:36 -0500
> From: Puneet Kishor <punk.kish at gmail.com>
> Subject: [OpenLayers-Users] need a clarification re styleMaps
> To: openlayers-users at lists.osgeo.org
> Message-ID: <7F9649FD-4474-4A89-8331-592D2A1CB73E at gmail.com>
> Content-Type: text/plain; charset=windows-1252
>
> The following code works
>
>     // Add styleMap to layer
>     var style_map = new OpenLayers.StyleMap({
>         externalGraphic: uri + "/img/arrow.png",
>         graphicWidth: 15,
>         graphicHeight: res[0].height,
>         rotation: res[0].angle
>     });
>     var lyr = new OpenLayers.Layer.Vector("name", {styleMap: style_map});
>
>     // Points without styleMap
>     var points = [];
>     points.push(
>         new OpenLayers.Feature.Vector(
>             new OpenLayers.Geometry.Point(res[0].lng, res[0].lat)
>         )
>     );
>     lyr.addFeatures(points);
>     map.addLayer(lyr);
>
> everything works. Different points get drawn with symbols of correct  
> height and rotation. But, if I don't add the styleMap definition to  
> the lyr when creating it, and instead, add it to the point, like so
>
>     // Layer with styleMap
>     var lyr = new OpenLayers.Layer.Vector("name");
>
>     // Add styleMap to each point
>     var style_map = new OpenLayers.StyleMap({
>         externalGraphic: uri + "/img/arrow.png",
>         graphicWidth: 15,
>         graphicHeight: res[0].height,
>         rotation: res[0].angle
>     });
>
>     var points = [];
>     points.push(
>         new OpenLayers.Feature.Vector(
>             new OpenLayers.Geometry.Point(res[0].lng, res[0].lat),
>             {styleMap: style_map}
>         )
>     );
>     lyr.addFeatures(points);
>     map.addLayer(lyr);
>
> then nothing gets drawn. At best, this is counter-intuitive to me…  
> the style is per point, not per layer. At worst, this creates a  
> problem for me that I don't know how to solve. I am creating a layer  
> that I want to reuse. If I follow the first approach above, the  
> first time around everything works well. But, on subsequent  
> iterations the points get drawn with the previously set style  
> (rotation and height).

I had a similar issue not that long ago. While I too find it odd that  
adding the stylemap to the point itself did not work, I got what I  
needed by adding a uniqueValueRule to the style that determined what  
style rules to use based on a an arbitrary property of the point.

http://docs.openlayers.org/library/feature_styling.html#adduniquevaluerules


mike



More information about the Users mailing list