2

How do I reference controls that have already been added to a panel. I am attempting to destroy or modify the default controls associated with the editingToolbar. These changes would be done outside of the initialization function.

EDIT:My intent was to change the layer that a drawFeature controls was adding features too.

2 Answers 2

2

When accessing controls that have been added to an OpenLayers panel, you will need to determine the index of the control in question and access the control via the panel.controls object.

My intent was to change the layer that a drawFeature controls was adding features too. The simple snippet below shows the call to change the layer being edited by a point feature.

//Simple function to find an index by array object attribute.
function findWithAttr(array, attr, value) {
   for(var i = 0; i < array.length; i++ ) {
      if(array[i][attr] === value) {
         return i;
       }
   }
}
//Get you controls array for the index function.
var controlArray = panel.controls; 
//This call accesses the controls on your panel(may not be named panel) by the index 
// of the displayClass of the control, and returns the layer that is linked to that control.
panel.controls[findWithAttr(controlArray, "displayClass", "olControlDrawFeaturePoint")].layer

After accessing the correct control you can simply reassign the layer of the control to a different layer using the below as an example.

panel.controls[0].layer = zlayer;

In that example the 0 is the index of the control which you would need to determine with the above sample.

2

Updated

You can get the control by using method.

ctrl = map.getControlsByClass("OpenLayers.Control.DrawFeature");

then reassign the layer

ctrl[0].layer = zlayer;
4
  • panel.controls[index] ended up being the reference I used. My target functionality was to change the layer that my drawfeature controls were drawing too. Changing panel.controls[1].layer = (openlayers.layer of choice) allowed me to change the layer that features were added too. Commented Aug 23, 2013 at 16:42
  • Did you Solve it?
    – neogeomat
    Commented Aug 23, 2013 at 16:57
  • See added answer. Commented Aug 23, 2013 at 16:58
  • 1
    Congratulations, but you didn't mention "My intent was to change the layer that a drawFeature controls was adding features too." in the question. You should modify your question as well.
    – neogeomat
    Commented Aug 23, 2013 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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