I have a web GIS where I use OpenLayers. I'm trying to make a FOR loop to generate a series of rules for my POI layer. I have tried just like that:


My problem is that i cannot access attribute values of my layer in 3 parameters that I need: 'title', externalGraphic, and value. Anybody can help me do that?


You are not supposed to pass features' attribute values to the title and filter of OpenLayers.Rule. The attribute values can only be used in features' styles, in OpenLayers.Rule it is the symbolizer that styles the features that meet the rules.

And I think you have some misunderstanding about OpenLayers.Rule. Rules are supposed to applied to layers, not features. The 'title' is the rule title (not from feature's attributes), and your filter just doesn't make any sense.

Maybe better to illustrate with an example, let's say we have a rule as following:

var rule = new OpenLayers.Rule({
    title: 'id_between_1_50',
    filter: new OpenLayers.Filter.Comparison({
        type: OpenLayers.Filter.Comparison.BETWEEN,
        property: "id",
        lowerBoundary: 1,
        upperBoundary: 50
    symbolizer: {
        label: "${name}",
        externalGraphic: "../../img/simbologia_layers/pdi/" + "${id}" + ".png"

If you apply this rule to the layer's styleMap, those features with an attribute 'id' from 1 to 50 will shown on map with the label as the 'name' attribute and graphic as id.png. This is an good example that show you how to use the rules.


I think you don't need to use rules at all if you just want to show POIs in the same way. You can just use the following stylemap, put the label and externalGraphic in styles:

var estilo_pdi = new OpenLayers.StyleMap({
    "default": {
        strokeColor: "#000000",
        strokeOpacity: .7,
        strokeWidth: 3,
        fillColor: "#ff0000",
        fillOpacity: 0,
        cursor: "pointer",
        label: "${name}",
        externalGraphic: "../img/simbologia_layers/pdi/" + "${id}" + ".png"
    "temporary": ...
  • Ok i understand but i have a layer with many POI's that i want to show. I want that all points have a different name in legend and a different image in 'externalGraphic'. What is the correct way of doing this? Thanks – pdcc Sep 20 '13 at 11:33
  • I understand now how i have to do for different graphics but and for the name in legend? – pdcc Sep 20 '13 at 11:36
  • With the example I've given, you can show different image for each POI, i.e. '${id}.png', and you can leave out the rule's filter if you want to show all POI by using the same symbolizer. The same symbolizer does not mean you show all POI in the same label or image, you can use the values from featuers' attributes, e.g. 'name', 'id', to customize the styling of your features in needed way. – mfdev Sep 20 '13 at 11:44
  • I'm doing like that now: pastebin.com/DmmgsVqB But it still don't get the image. Openlayers don't get the value of attribute 'id'... – pdcc Sep 20 '13 at 14:18
  • I've update the answer :) – mfdev Sep 20 '13 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.