[Users] Displaying customs icons in one layer

Tim Schaub tschaub at opengeo.org
Tue Mar 23 23:24:43 CET 2010


Pol wrote:
> Hello,
> 
> I just discovered GeoExt and OpenLayers 2 days ago and I'm rebuilding my
> project using GeoExt (it was using the very good GmapPanel before).
> First of all, thanks for it, it's a very good job !
> 

Thanks.  Welcome.

> I subscribe to this list because I have a small problem.
> 
> I have a single URL who return me all the markers to display on the map.
> Those markers must have different icons.
> 
> I don't want to create multiple layers for each kind of markers, that would
> make too much external calls and slow down the whole stuff.
> 
> I have pasted some bits of code here: http://pastebin.com/Uee7xdzJ
> 
> I suppose that I have to add a property like:
> 
> imgRef: 1
> 
> in my json response to identify the picture to display in javascript later,
> but I don't know where to put it and how to retrieve it.
> 
> Can you help me ?

Your vector layer will contain a collection of vector features. 
OpenLayers.Feature.Vector objects have an "attributes" property.  You'll 
need to have a feature attribute that can be used to derive the path to 
the image you'd like to use for rendering those features.

In the simplest case, your features have an attribute whose value is the 
path to an external graphic.  In this case, you will be using a 
symbolizer that refers to the appropriate feature attribute.

e.g.

     // assuming feature.attributes.icon = "path/to/marker.png"
     var symbolizer = {
         graphicHeight: 10,
         graphicWidth: 5,
         externalGraphic: "${icon}"
     };

This symbolizer is used to create an OpenLayers.Style object that is 
referenced in an OpenLayers.StyleMap (yes, complicated).

See http://openlayers.org/dev/examples/styles-rotation.html for a 
similar example using feature attribute values for rotation and fill 
opacity.

It looks like you are using GeoJSON as your transport format.  Your 
OpenLayers feature object would look something like the following when 
serialized:

     {
         "type": "Feature",
         "geometry": {
             "type": "Point",
             "coordinates": [4.22, 50.72]
         },
         "properties": {
             "icon": "path/to/marker.png"
         }
     }

The "properties" member of a GeoJSON feature object maps to the 
attributes object of an OpenLayers vector feature.

If you don't want to include the path to your external graphic in your 
data, but instead you want to render your features based on some other 
combination of attributes, you can use rule based styling.  Write back 
if that's what you're after.

And, you might find the OpenLayers lists a more appropriate place for 
pure OpenLayers questions.

Good luck,
Tim


> 
> Thanks.
> 
> -Pol D.-
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Users mailing list
> Users at geoext.org
> http://www.geoext.org/cgi-bin/mailman/listinfo/users


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


More information about the Users mailing list