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 am quite new to OpenLayers. Right now, I have a polygon vector with some styling applied and a label.

var style = $.extend(true, {}, OpenLayers.Feature.Vector.style['default']);
style.pointRadius = 15;
style.label = "My Polygon";
style.fillColor = #f00;
style.strokeColor = #000;

var styleMap = new OpenLayers.StyleMap({"default" : style});

var polygonLayer = new OpenLayers.Layer.Vector("Polygon Layer", {styleMap: styleMap});

At some point after doing some processing, I want to display the result as a label. How can I update the label? I figure it would be something like this

polygonLayer.options.styleMap.styles.label = "Updated label";

Thanks in advance.

share|improve this question
add comment

1 Answer 1

up vote 5 down vote accepted

The right syntax is like :

vectorlayer.features[1].style = {label: "Updated label"};

Here comes a full piece of code :

       <script type="text/javascript">
        // few sample data
        var box_extents = [
            [-10, 50, 5, 60],
            [-75, 41, -71, 44],
            [-122.6, 37.6, -122.3, 37.9],
            [10, 10, 20, 20]
        ];

        var map;

        function init(){           

            map = new OpenLayers.Map('map');

            // base layer
            var ol_wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
                "http://vmap0.tiles.osgeo.org/wms/vmap0?", {layers: 'basic'} );
            // default style layer
    var style = new OpenLayers.Style({
        fillColor: "#FCC764",
        strokeColor: "#E5BE12",
        strokeWidth: 1,
        label: "My label"
    });

            var boxes  = new OpenLayers.Layer.Vector( "Boxes" ,{
        styleMap: new OpenLayers.StyleMap(style)
    })         

            for (var i = 0; i < box_extents.length; i++) {
                ext = box_extents[i];
                bounds = OpenLayers.Bounds.fromArray(ext);

                box = new OpenLayers.Feature.Vector(bounds.toGeometry());
                boxes.addFeatures(box);
            }

            map.addLayers([ol_wms, boxes]);
            map.zoomToMaxExtent();

    // you update the desired label
    boxes.features[1].style = {label: "Updated label"};
    // you redraw the right layer
    map.layers[1].redraw();
        }
    </script>

And you will find an example online.

share|improve this answer
add comment

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.