1

I am trying to remove custom draw controls from my OpenLayers application which I create like this:

var drawControls = {
     polygon: new OpenLayers.Control.DrawFeature(polygonLayerFreehand, OpenLayers.Handler.Polygon),
     select: selectControl,
     box: new OpenLayers.Control.DrawFeature(boxLayer,
          OpenLayers.Handler.RegularPolygon, {
            handlerOptions: {
                sides: 4,
                irregular: true
            }
        }
    )
};

and which I add to the map like this:

for(var key in drawControls) {
            _map.addControl(drawControls[key]);
            }

I now would like to know how I can remove this custom control. I found examples for removing the standard OL controls like e.g. this

var ovMapControl = _map.getControlsBy("CLASS_NAME", "OpenLayers.Control.OverviewMap")[0];
_map.removeControl(ovMapControl);

or

var ovMapControl = _map.getControlsBy('displayClass','olControlPanZoomBar')[0];
 _map.removeControl(ovMapControl);

but what is the 'displayClass' or the 'CLASS_NAME' in the case of custom controls like my 'drawControls'?

0

Assume you still have access to drawControls object at the place you want to remove the controls, then you can do something like:

for (var key in drawControls){
    _map.removeControl(drawControls[key]);
}
0

You can simply remove control by using

_map.removeControl(drawControls[key]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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