[GIS] How to disable polygon Label on the vertices (OpenLayers) in Edit Mode

labelingopenlayers-2polygon

I added the style on the polygon layer like so.

var style = $.extend(true, {}, OpenLayers.Feature.Vector.style['default']); // get a copy of the default style
style.label = "1233456 ha"
style.fillOpacity = 0.1
style.strokeWidth = 3
var styleMap = new OpenLayers.StyleMap({"default": style});
polygonLayer = new OpenLayers.Layer.Vector("Polygon Layer", {styleMap: styleMap});

The polygon looks fine on normal mode.

label on normal mode

However, on ModifyFeature, labels are showing up on every vertex.

enter image description here

So, I test this http://trac.osgeo.org/openlayers/ticket/2176.

var styleMap = new OpenLayers.StyleMap(new OpenLayers.Style({
        label: "${getLabel}"
        // your other symbolizer properties here
    }, {context: {
        getLabel: function(feature) {
            if(mycontrolIsNotInEditMode) {
                return feature.attributes.label;
            }
        }
    }}
));

Then the labels on the vertices are disabled, BUT the label between vertices are still showing up.

how can I disable the labels between the vertices?
Thank you..

Best Answer

You can make sure that the geometry is of type polygon so that it only gets the label if it's a polygon and not the line or point. By adding && feature.geometry.CLASS_NAME == "OpenLayers.Geometry.Polygon" to your if statement. That should work.

Like this:

...
getLabel: function (feature) {
    if (mycontrolIsNotInEditMode && feature.geometry.CLASS_NAME == "OpenLayers.Geometry.Polygon") {
        return feature.attributes.label;
    }
}
...

Update:

Here is a Working Example

This is the important part:

var style = $.extend(true, {}, OpenLayers.Feature.Vector.style['default']); // get a copy of the default style
style.label = "${getLabel}";
style.fillOpacity = 0.1
style.strokeWidth = 3

var styleMap = new OpenLayers.StyleMap({
    "default": new OpenLayers.Style(style, {
        context: {
            getLabel: function (feature) {
                if (feature.geometry && feature.geometry.CLASS_NAME == "OpenLayers.Geometry.Polygon") {
                    return "1233456 ha";
                } else {
                    return "";
                }
            }
        }
    })
});  

So that it only returns a label if it's a polygon: enter image description here