1

I need to use Jquery to handle other elements on the webpage. I tried to put labels on a vector layer with the following style:

  var vector_style = new OpenLayers.Style({
            'fillColor': 'red',
            'fillOpacity': .8,
            'strokeColor': '#aaee77',
            'strokeWidth': 3,
            'pointRadius': 8,
            'label': "${name}",
            'labelAlign': "cc",
            });

But the labels do not show.

I am just wondering if there is a conflict on this one "${name}". I tested it again with a simple page without Jquery. The labels do show. Please let me how to fix this. Thanks.

Update: I just figured out the conflict may come from Spring or JSTL. I use Firebug tool to check the javaScript source code of the style. It appears as this:

    var vector_style = new OpenLayers.Style({
            fillColor: 'red',
            fillOpacity: .8,
            strokeColor: '#aaee77',
            strokeWidth: 3,
            pointRadius: 8,
            label: "",
            labelAlign: "cc",
            });

I returned an variable from the server side:

    model.addAttribute("name", "test");

then all features show the label "test". It approved that ${name} is recognized as a spring variable.

4
  • have you checked your browser's console for errors? Perhaps that will help you/us understand what's going on. On IE you can access the console with F12, on chrome with CTRL+SHIFT+i
    – unicoletti
    Commented Dec 21, 2012 at 17:04
  • Usually there is no conflicts, when using jQuery and OL's "${name}" type styling together. For testing, turn 'label': "${name}" into 'label': 'foo' and check, whether labels show up or not. Commented Dec 21, 2012 at 17:07
  • I checked with the firebug. map.layers[0].styleMap.styles.default.defaultStyle.label returns null and map.layers[0].features[0].attributes.name returns the value.
    – Alex
    Commented Dec 21, 2012 at 21:22
  • if I use ` 'label': 'foo' `,the labels show up.
    – Alex
    Commented Dec 21, 2012 at 21:25

2 Answers 2

1

I put the javascript code into a separate *.js file now. This avoids the conflict between the spring $ variable and openlayers $ variable.

0

No. jQuery and OpenLayers use separate name spaces.

I guess you not providing a value to the label attribute of your Feature object. Did you know that?

Also you do not need put 'quotes' in your object label names. You can write this instead:

label: "${name}"

A feature object has the following constructor:

OpenLayers.Feature.Vector( geometry, attributes )

To you configuration work the attributes variable above must contain a label property like on example below:

{ name: 'this will show' }
2
  • if I use the fixed label, like ` 'label': 'foo`, it will show. and i can get the value from map.layers[0].features[0].attributes.name
    – Alex
    Commented Dec 26, 2012 at 20:32
  • You ca use the "value" of attribute name as the label with the following as already explained on answer: 'label': '${name}'. This will make OpenLayers search for the property name into feature been drawed.
    – cavila
    Commented Dec 27, 2012 at 14:36

Your Answer

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

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