5

I have a Layer:

          pointLayer = new OpenLayers.Layer.Vector("Point Layer", {
            strategies: [strategy],
            styleMap: new OpenLayers.StyleMap({
                "default": {
                    label : "${text}",
                    pointRadius: 10,
                    fillColor: "#ffcc66",
                    fillOpacity: 0.8,
                    strokeColor: "#cc6633",
                    strokeWidth: 2,
                    strokeOpacity: 0.8
                },
                "select": {
                    fillColor: "#8aeeef",
                    strokeColor: "#32a8a9"
                }
            })
        });  

I create this layer without any feature. I periodically call the server and get json objects from it. The json objects have coordinate attributes and a text attribute. After i get the objects, i put points on the layer. I set the text attribute in the point features:

pointFeature.attributes = {
    text: jsonObj.text
};

The problem is that the labels don't appear on the layer (but the points appear). I noticed that the labels appeared only when i added the point features to the layer before i added the layer to the map.

I found the redraw() method, but it doesn't work because after the server call i remove all the points from the layer and add the new points to it.

To summarize the problem: i want to display labels of point features which were added to the layer after the layer was added to the map.

1

try to attach a style to each feature (point) before adding it to the layer. http://dev.openlayers.org/docs/files/OpenLayers/Feature/Vector-js.html#OpenLayers.Feature.Vector.style

eg

pointFeature.style = {
                label : "${text}",
                pointRadius: 10,
                fillColor: "#ffcc66",
                fillOpacity: 0.8,
                strokeColor: "#cc6633",
                strokeWidth: 2,
                strokeOpacity: 0.8
            }
1

byom, thanks for your answer. i examined the code and realized that the problem is not the ${text} part. When i got the server response, i did the following steps:

  • removed the features with pointLayer.removeAllFeatures() method
  • in a for loop i processed the json objects and made the point features and collected them in an array
  • after the for loop i added them to the pointLayer with: pointLayer.addFeatures(points)

On the map the points appeared with "undefined" label. All the texts of the labels were literally "undefined". Than i tried to add the point features to the layer in other way. In the for loop after i created a point feature i added it to the layer with pointLayer.drawFeature(pointFeature). And it worked. The labels appeared with the correct texts. Unfortunately, i don't know the correct explanation, but it works.

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.