[OpenLayers-Users] Setting the color of a single vector

Arnd Wippermann arnd.wippermann at web.de
Tue Aug 5 12:58:56 EDT 2008


I use this code to change the style for selected features.

//selected features get the new style

function bunt()
{
    var vlyr= opener.map.layers[opener.map.aktLayer];
    //output=vlayer.name + ', isVector=' + vlayer.isVector;
    if(vlyr.isVector==true);
    {
        var objFs = vlyr.selectedFeatures;

        for(var i=0;i<objFs.length;i++)
        {
            var feature = objFs[i];
            
            var obj     = new opener.OpenLayers.Style();
    
            obj.fillColor     = "blue";
            obj.fillOpacity   = 0.5;
            obj.strokeColor   = "#000000";
            obj.strokeOpacity = 1;
            obj.strokeWidth   = 6;
            obj.strokeLinecap = 'round';
            obj.pointRadius   = 2;
    
            feature.style = obj;
            
            vlyr.drawFeature(feature);
        }
    };
}
//bunt(); this way sets only the above style values

function BuntOnSelect()
{
    var vlyr= map.layers[idx];
    var objFs = vlyr.selectedFeatures;

    var myStyle = OpenLayers.Util.extend({},
map.layers[idx].styleMap.styles["default"].defaultStyle);

    myStyle.strokeColor = "#FF00FF";
    //... other

    for(var i=0;i<objFs.length;i++)
    {   objFs[i].style = myStyle;
        vlyr.drawFeature(objFs[i]);
    }
}
BuntOnSelect(); //this way all style values are taken from the default of
the layer style and then you can change yours.

Look also:
http://www.nabble.com/Change-style-only-for-one-feature-on-a-layer-td1834918
0.html

Arnd Wippermann

-----Ursprüngliche Nachricht-----
Von: users-bounces at openlayers.org [mailto:users-bounces at openlayers.org] Im
Auftrag von Jani Patokallio
Gesendet: Dienstag, 5. August 2008 11:05
An: users at openlayers.org
Betreff: [OpenLayers-Users] Setting the color of a single vector

Greetings,

A very simple question, probably with a very simple answer, but I'm having a
devil of a time figuring it out...

I have a PointTrack layer with a StyleMap attached to it.  The StyleMap says
strokeColor: yellow, so when I draw vectors on the layer, they're yellow.
My question is, how do I specify that one (1) vector should
*not* be yellow, but red?

Feature.Vector has style objects as an optional argument, but I have no idea
what "style {Object} An optional style object" is supposed to mean, and my
guess at "new Vector(point, {foo: "bar"}, {strokeColor: "red"})" 
does nothing.  Google searches dig up people extending the default style and
then defining a second, entirely separate layer, while
http://trac.openlayers.org/wiki/Styles goes straight off the deep end into
"advanced rule-based styling", but all I want to do is draw one red line.
Help?

Cheers,
-jani

_______________________________________________
Users mailing list
Users at openlayers.org
http://openlayers.org/mailman/listinfo/users




More information about the Users mailing list