[OpenLayers-Users] Override layer default style for feature

emmexx emmexx at tiscalinet.it
Sat Jan 9 08:41:55 EST 2010


I defined the style for a layer the following way:

var styleDefault=new OpenLayers.Style(null,
{
 rules: [
  new OpenLayers.Rule({
   symbolizer: {
    "Point": {
      pointRadius: 5,
      graphicName: "square",
      fillColor: "yellow",
      fillOpacity: 0.25,
      strokeWidth: 1,
      strokeOpacity: 1,
      strokeColor: "#333333",
     },
     "Line": {
      label: "someText",
      fontColor: somecolor,
      labelAlign: "${labelPosition}",
      fontSize: "12px",
      fontWeight: "bold",
      strokeColor: "${getColor}", // using context.getColor(feature)
      strokeDashstyle: "${getDash}", // using context.getDash(feature)
      strokeWidth: 4,
      strokeOpacity: 0.9
     }
    },
    elseFilter: true
   })
  ],
 context: context	
});

var myStyle = new OpenLayers.StyleMap(
{
 'default': styleDefault,
 'temporary': styleTemp,
 'select': styleSelect,
});

var myLayer = new OpenLayers.Layer.Vector(
 "a name",
{
 projection: map.displayProjection,
 styleMap: myStyle
});

context is the hash of functions to retrieve the values for the color
and the dash style of lines drawn on the layer by the users.

All of this works ok.

Now I want to draw an icon on the layer. I tried the following:

var ctrl = new OpenLayers.Control.DrawFeature(
 layer, OpenLayers.Handler.Point,
 {
  style:
  {
   externalGraphic: "../img/myimage.png",
   graphicWidth: 24,
   graphicHeight: 22,
   graphicYOffset: -11
  }
 }
)

and this puts a square on the map (as per "Point" rule above, myImage
isn't a square.)

And variations of:

var tmp = new OpenLayers.Feature.Vector(
 new OpenLayers.Geometry.Point(x,y),
 null,
 myStyle
);
layer.addFeatures(tmp);

defining myStyle as:
mystyle= new OpenLayers.Style(
{
 externalGraphic: "../img/myimage.png",
 graphicWidth: 24,
 graphicHeight: 22,
 graphicYOffset: -50
});

putting it inline and trying all the OL examples related to styling.

I even tried:
drawFeature(tmp, myStyle)
or defined tmp.style=myStyle
but nothing appears on the layer.
In the resulting html there's the following:

<image id="OpenLayers.Geometry.Point_2096" cx="149.99999999159627"
cy="275.99999999825377" r="1" preserveAspectRatio="none" x="138" y="265"
width="24" height="22" href="../img/myImage.png" style="opacity: 0.25;"
fill="yellow" fill-opacity="0.25" stroke="#333333" stroke-opacity="1"
stroke-width="1" stroke-linecap="round" stroke-linejoin="round"
stroke-dasharray="none" pointer-events="visiblePainted" cursor="inherit"/>

but myImage is not visible.

>From the docs I inferred that it's possible to force a style on a
feature, overriding the layer default style. Is this correct?
How can I draw an icon on that layer?

thanks
	maxx



More information about the Users mailing list