In my (limited) experience, there are at least two effective ways of
doing this.
1. A: Use a stylemap with "default" and "select" styles in it. eg:
var myStyles = new OpenLayers.StyleMap({
"default": new OpenLayers.Style({
strokeWidth: 2,
fillColor: "#00cc00",
fillOpacity: 0.5,
strokeOpacity: 0.7,
pointRadius: 19,
graphicZIndex: 1,
label: "${lab}",
labelYOffset: 17,
}
),
"select": new OpenLayers.Style({
strokeWidth: 3,
fillColor: "#66cc00",
strokeColor: "#ffff99",
graphicZIndex: 7
})
});
B: Then, add add it to your layer:
posLayer = new OpenLayers.Layer.Vector('posLayer',{
styleMap: myStyles
});
C: and add a controller to tell the layer to stay awake!
select = new OpenLayers.Control.SelectFeature(
posLayer,
{
hover:"true",
// there are other cool options you can add here
}
);
// ... add to the map
map.addControl(select);
2. The other thing you can do is use a variable attribute.
Define your fill color as a variable: fillColor: "${color}",
and programmatically set [featurename].attributes.color to be the value
you want.
However, if you want mouseover treatment, you will likely need to come
back to the tools of method 1, so I'd start with that.
Good luck.
Michael
On 10/11/2010 11:06 AM, William Martin wrote:
All,
I've got some vector features, and I'd like to change their opacity when the
user mouses over a link in a side-menu. The map always has a set number of
features (17 county outlines) with predictable feature indices, so I can get
appropriate vector feature easily enough:
vector = new OpenLayers.Layer.Vector("Vector Layer", {style:
OpenLayers.Feature.Vector.style['default']});
for(i = 1; i< counties.length; i++){
cur = counties[i];
var details = {name: cur.name, url: cur.url}
// Prep some attribues.
attributes = {name: cur.name};
style = cur.style; // The initial styles are set in the counties array.
// Draw the boundaries of the county.
vertices = new OpenLayers.Geometry.LinearRing(cur.bounds);
counties[i] = new OpenLayers.Feature.Vector(new
OpenLayers.Geometry.Polygon(vertices), details, style);
vector.addFeatures(counties[i]);
}
... and then I can get the county with vector.features[0] (or 1, 2, 3, etc).
So now that I've got that, how do I set the opacity of the selected feature?
I've tried this:
vector.features[0].style.fillOpacity = 0.5;
But it has no effect. I tried this to get a readout of all the properties and
methods of the feature:
for(i in vector.features[0]){ console.log(i); }
But it doesn't show a setOpacity method or anything. What am I missing, please?
Thanks.
Will Martin
_______________________________________________
Users mailing list
[email protected]
http://lists.osgeo.org/mailman/listinfo/openlayers-users
_______________________________________________
Users mailing list
[email protected]
http://lists.osgeo.org/mailman/listinfo/openlayers-users