Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am quite new to OpenLayers. Right now, I have a polygon vector with some styling applied and a label.

var style = $.extend(true, {},['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
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",
                "", {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());

            map.addLayers([ol_wms, boxes]);

    // you update the desired label
    boxes.features[1].style = {label: "Updated label"};
    // you redraw the right layer

And you will find an example online.

share|improve this answer

Your Answer


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.