[OpenLayers-Users] Change color on a feature?

Alexandre Dube adube at mapgears.com
Wed Sep 17 09:02:18 EDT 2008


Hi Swärd,

  Here's a simple example of how it can be done.  ( Note that I just 
copied/pasted some of my code and changed some parts of it so it might 
have some errors, but the objective here is to make you understand how I 
did it )...

  The following will work with a selectFeature Control activated and a 
html <select> element of colors.  Let's say you have some drawn 
features.  Select one, then select a color from the <select> menu...  It 
will redraw your selected feature with the chosen color.

Hope this helps,

Alexandre

<!-- HTML PART -->

<select id="colors" onchange="drawFeatureUsingColor(this.value);">
     <option value="purple">purple</option>
     <option value="blue">blue</option>
     <option value="yellow">yellow</option>
     <option value="red">red</option>
     <option value="orange">orange</option>
     <option value="green">green</option>
</select>

// JS PART

aColors = {
    'purple': { fillColor:"purple" },
    'blue'  : { fillColor:"blue" },
    'yellow': { fillColor:"yellow" },
    'red'   : { fillColor:"red" },
    'orange': { fillColor:"orange" },
    'green' : { fillColor:"green" }
};

function drawFeatureUsingColor(szColor){
    // get current selected feature
    oFeature = olWFS.events.listeners.featureselected[0].obj.feature;
    // draw the feature with chosen color
    olWFS.drawFeature(oFeature, aColors[szColor]);
}

Swärd Mårten wrote:
>
> Hello!
>
> Is it possible to change the color of a feature that is already drawn 
> to the map?
>
> If so, how do I do it? J
>
> I have a WFS layer and I need to change the colors of the feature 
> depending of values that the user decides..
>
> ************************************************************************
>
> *Mvh:*
>
> Mårten Swärd
>
> Systemutvecklare/Webbutvecklare, ITs
>
> SMHI, Sveriges Meteorologiska och Hydrologiska Institut
>
> Tel: +46 11 495 8469
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users
>   


-- 
Alexandre Dubé
Mapgears
www.mapgears.com




More information about the Users mailing list