[GIS] Removing custom controls

mapcontrolopenlayers-2

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'?

Best Answer

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]);
}