1

I need to show label of each feature based on attribute field value. I know that I can use ${attribute} notation for this purpose. But this approach works only if I make StyleMap and pass it to vector layer constructor. But in this case I can not modify style of each features separately. How I can make layer with labels and with not null value of style property of each feature?

My current config:

var custom_style = {
 fill: true,
 fillColor: "#feb24c",
 fillOpacity: 0.2,
 strokeColor: '#3182bd',
 strokeWidth: 1
};
var l = new OpenLayers.Layer.Vector("Coverage", {
 strategies: [new OpenLayers.Strategy.BBOX()],
 protocol: new OpenLayers.Protocol.HTTP({
 url: "http://***:8088/collect",
 format: new ol.Format.GeoJSON()
 }),
 style: custom_style
});

If I add label: ${attribute} to custom_style object, then text ${attribute} will be shown on map, not value of attribute.

Shiko
2,90315 silver badges23 bronze badges
asked May 28, 2013 at 19:38
1
  • are you having problems because some features have null value? Commented Oct 28, 2013 at 13:54

2 Answers 2

1

If you want to process each feature and display something different depending on the value of a feature attribute, you can supply a function via the style context that returns different values. Here's an example. However, this approach does use a style map - I'm not sure if you want to avoid using one for some reason.

The value of the label is generated by the assigned function.

answered May 12, 2014 at 23:19
1
  • Unfortunately, you can't. Variables are only evaluated in StyleMap objects. Commented Aug 23, 2015 at 10:42
0

In the case of the label, probably this may work:

label: '${attribute}'

Here I modified your code with it:

var custom_style = {
 fill: true,
 fillColor: "#feb24c",
 fillOpacity: 0.2,
 strokeColor: '#3182bd',
 strokeWidth: 1,
 label: '${attribute}'
};
var l = new OpenLayers.Layer.Vector("Coverage", {
 strategies: [new OpenLayers.Strategy.BBOX()],
 protocol: new OpenLayers.Protocol.HTTP({
 url: "http://***:8088/collect",
 format: new ol.Format.GeoJSON()
 }),
 style: custom_style
});
answered May 28, 2013 at 20:54
2
  • Unfortunately, this approach doesn't work as I mentioned above. Commented May 29, 2013 at 1:54
  • could you post some images showing what you have and what you expect? Commented May 29, 2013 at 10:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.