[OpenLayers-Dev] What if I extend the OpenLayers.Style class??

Tim Schaub tschaub at opengeo.org
Wed Jan 21 04:11:55 EST 2009


Hey-

bartvde at osgis.nl wrote:
> Hi,
> 
> see also:
> 
> http://trac.openlayers.org/ticket/1802
> 
> Best regards,
> Bart
> 
>> Hi all,
>> In my OL app, I needed to create some OpenLayers.Style class to group
>> complex layer styling.
>> I create some classes like OpenLayers.Style.Rain,
>> OpenLayers.Style.Temperature to represent some
>> weather icons depending on some feature's attribute values.

Yes, you'll get good behavior with instanceof after r8445.  However, I 
think you may be going about this the wrong way.

You shouldn't need to extend the Style class.  Instead you create a 
Style object with rules that determine the styling for your particular case.

e.g. something like:

var rainStyle = new OpenLayers.Style();
rainStyle.addRules([
     new OpenLayers.Rule({
         filter: new OpenLayers.Filter.Comparison({
             type: OpenLayers.Filter.Comparison.EQUAL_TO,
             property: "rain"
             value: "lots"
         }),
         symbolizer: {
             externalGraphic: "wet.png"
         }
     })
]);

You can also use the addUniqueValueRules convenience method on your 
StyleMap [1].  Or check out the other style examples for inspiration [2].

Tim

[1] http://openlayers.org/dev/examples/styles-unique.html
[2] http://openlayers.org/dev/examples/?q=style

>>
>> But, I had an other problem, in the OpenLayers.StyleMap class passed to my
>> weather layer.
>> I found this code in the initialize function:
>>
>> *if(style instanceof OpenLayers.Style)*
>>
>> This code block my Rain or Temperature classes to go further.
>>
>> So I changed the code in something like:
>>
>> *if(style instanceof OpenLayers.Style ||
>>                (style.CLASS_NAME &&
>> style.CLASS_NAME.indexOf("OpenLayers.Style")!=-1))*
>>
>> Does it make sense to you?
>>
>> Thanks
>>
>>   Milan Antonovic
>>
>>
>> Milan Antonovic
>> Institute of Earth Sciences
>> University of Applied Sciences of the South Switzerland - SUPSI
>> Trevano, C.P. 72, CH-6952 Canobbio, SWITZERLAND
>> Tel: +41(0)58 666 62 13
>> E-mail: milan.antonovic at supsi.ch
>> Web: http://www.ist.supsi.ch
>> _______________________________________________
>> Dev mailing list
>> Dev at openlayers.org
>> http://openlayers.org/mailman/listinfo/dev
>>
> 
> 
> _______________________________________________
> Dev mailing list
> Dev at openlayers.org
> http://openlayers.org/mailman/listinfo/dev


-- 
Tim Schaub
OpenGeo - http://opengeo.org
Expert service straight from the developers.



More information about the Dev mailing list