2

I am trying to get the active state of an OpenLayers SelectFeature control. I create this control and add it to my map but I do NOT activate it yet. This part works fine. Here is the script to create the control and add it to the map:

var highlightControl = new OpenLayers.Control.SelectFeature(lyrstands, {
        hover: true,        
        highlightOnly: true,                
selectStyle: {
   fill: "#0000FF",
           strokeColor: "#0000FF",
           strokeWidth: 2,
   fillOpacity: 0.5
          }
    });


map.addControl(highlightControl);

Then I have a button that calls a function. Here is the code for the button:

new OpenLayers.Control.Button({title: "Select Polygon", trigger: HiLite})   

I know the button is calling the HiLite function because if I put a simple alert statement in the HiLite function (for example: alert("The Function Works");) it works correctly.

So what I am trying to do is check to see if highlightControl has been activated by testing the highlightControl.active value, if it is already activated the function should do nothing, but, if highlightControl is NOT activated it should activate it. Unfortunately, the function does not work. Here is the function I have written:

function HiLite(){
  if (highlightControl.active==false)       
          {highlightControl.activate();}
  else {}
   } 

If I write the function to simply activate the tool without the If Else stuff it works fine. But, for some reason when written as above I cannot get the status of the Active attribute. The OpenLayers documentations states that the Active attribute is a Boolean value and I assumed I could check the status using the highlightControl.active==false statement.

Why can I not get this function to work?

2 Answers 2

5

Maybe there is a problem with the visibility of your highlightControl variable. You could try to add an ID to your control, and than access it like this:

var highlightControl = new OpenLayers.Control.SelectFeature(lyrstands, {
    //add ID
    id: 'Mycontrol',
    hover: true,        
    highlightOnly: true,                
selectStyle: {
fill: "#0000FF",
       strokeColor: "#0000FF",
       strokeWidth: 2,
fillOpacity: 0.5
      }
});
//then do something like this
var control = map.getControlsBy("id", "Mycontrol")[0];
    if (!control.active) {
         control.activate();
     }

EDIT

This problem was solved thanks to SirijuS. Here is the code fix:

var highlightControl = new OpenLayers.Control.SelectFeature(lyrstands, {        
    id: 'ctrlHiLite',                
    hover: true,        
            highlightOnly: true,                     
    selectStyle: {
       fill: "#0000FF",
               strokeColor: "#0000FF",
               strokeWidth: 2,
       fillOpacity: 0.5
              }
        });

function HiLite() {

var control = map.getControlsBy("id", "ctrlHiLite")[0];
    if (!control.active) {
     control.activate();
 }
}
3
  • SirijuS, thank you for your response! You have solved the problem. Here is the code I used: Commented Oct 25, 2012 at 20:19
  • var highlightControl = new OpenLayers.Control.SelectFeature(lyrstands, { id: 'ctrlHiLite', hover: true, highlightOnly: true, selectStyle: { fill: "#0000FF", strokeColor: "#0000FF", strokeWidth: 2, fillOpacity: 0.5 } }); function HiLite() { var control = map.getControlsBy("id", "ctrlHiLite")[0]; if (!control.active) { control.activate(); } } Commented Oct 25, 2012 at 20:20
  • I'm glad i could help.
    – SirijuS
    Commented Oct 25, 2012 at 22:50
1

highlightControl.active starts out being null so checking for false will not work.

Use this instead:

if (!highlightControl.active) {
    highlightControl.activate();

} 
1
  • CaptDragon - Thank you for your response. Unfortunately, I was not able to get your script to work. I appreciate you taking the time to respond to my problem and hope you have a great day! Commented Oct 25, 2012 at 20:32

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.