Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
    
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 Dec 21 '12 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. –  user1702401 Dec 21 '12 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 Dec 21 '12 at 21:22
    
if I use ` 'label': 'foo' `,the labels show up. –  Alex Dec 21 '12 at 21:25

2 Answers 2

up vote 1 down vote accepted

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

share|improve this answer

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' }
share|improve this answer
    
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 Dec 26 '12 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 Dec 27 '12 at 14:36
    
I posted updates. Please help. Thanks. –  Alex Dec 27 '12 at 16:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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