2

I want to add the functionality of switching the option to pan the map and put it in a checkbox. But I'm not quite sure how I can do that.

The reason I want to do this is: I have a layer which displays features and has a select and hover control. So there's no way to pan the map, because the clickEvent triggers direct action from the Handlers (click,hover). I want to add a checkbox where I can activate/deactivate the possibility to pan the map, so I can pan the map when there are features present, which normally would trigger a reaction to the mouseover the feature.

The way I've tried to do it is this one but it didn't work. I attached the function to the checkbox but it never worked.

function allowPan() {

    if(panAct == false){
            highlightCtrl.activate();
            selectedControl.activate();
            alert("controls activated!!")
            panAct = true;
        }     
    if(panAct == true){ 
        highlightCtrl.deactivate();
        selectedControl.deactivate();
        alert("controls deactivated!!");
        panAct = false;
        }                  
}
6
  • 3
    we can't help you with only that's small pies of code. Please, post the complete code or give an url. By the way, working with javascript, don't forget to use firebug as debugging tool.
    – simo
    Commented May 5, 2011 at 10:04
  • I have this problem too. I tried to deactivate the Navigation and DragPan controls, but it didn´t worked. On this example the map curiosly can´t be panned when we are drawing a regular polygon feature, but i modified it and when I draw a line or a point the map can be panned. openlayers.org/dev/examples/regular-polygons.html (Leandro from Brazil)
    – user2882
    Commented May 5, 2011 at 13:05
  • Leandro, this is not an answer. For more clarity and to follow SE rules, you should have posted this as comment. see the FAQ for more information : gis.stackexchange.com/faq
    – simo
    Commented May 5, 2011 at 13:43
  • converted answer to comment with @simo reply
    – Mapperz
    Commented May 5, 2011 at 14:34
  • 1
    Ive uploaded a screenshot so you can actually SEE what i mean. bit.ly/kWDEpd , the yellow feature in the middle is the one where my mouse currently sits. if i would click i would activate a popup. i want to use the checkbox that you can see, to ACTIVATE or DEACTIVATE the controls that i have added to the map. Because a simple .deactivate (as shown in my example) didnt work. thanks :)
    – 1amtoo1337
    Commented May 5, 2011 at 14:59

2 Answers 2

2

It will be tricky to debug that without the full code. How could we know if your variables are globals, if you're calling the right control vars and so on?

Anyway, the first thing to do would be to test the value of panAct var by adding alert(panAct); instructions just at the beginning of your function.

By the way, it would probably be a great idea to return panAct; var at the end.

4
  • Yes the controls are global.ive tried this already and it worked so far that the value got changed from false to true and vice versa but the .deactivate Part never executed so the controls never got deactivated. But an alert after the .deactivate Part showed up so i dont think that you can just get rid of a control like that :( also (sorry) i wouldnt know what it would help if i would post my selectControl and/or my hovercontrol since its irrelevant what gets done in them.i want to implement functionality to deactivate those,so whats inside doesnt matter.And yeah,thanks for the return idea.
    – 1amtoo1337
    Commented May 5, 2011 at 15:50
  • ok, another way to do is to try first to deactivate your control outisde the function. Once it's done, ok to wrap it in a function again!
    – simo
    Commented May 5, 2011 at 15:56
  • ok i will try it this way.
    – 1amtoo1337
    Commented May 5, 2011 at 16:06
  • Hey folks ive got it fixed. My problem was that i did not add a return inside the if-statement. The way i constructed my function it goes inside the function, checks if panAct is false(which it is by default) then deactivates the controls and sets panAct to true, the next if-statement checks for true and after that activates all the controls again. Ive added a return inside every statement and my checkbox to activate/deactivate panning while having features present works.
    – 1amtoo1337
    Commented May 5, 2011 at 16:19
0

This is my final working version of the function which I've now added to my program. I think my boss will be making a joyful leap when he enters office tomorrow. Thanks @simo for pointing me in the right direction.

var panAct = new Boolean(); //panning activator
panAct = false; //panning not activated by default

//this is the function which is connected to the checkbox
//this gets fired when you check/uncheck the box

 function allowPan() {
    alert("in allowPan")
    alert("panAct:" + panAct);

    if(panAct == true){ 
        highlightCtrl.activate();
        selectedControl.activate();
        alert("controls activated!! Panning deactivated!!");
        panAct = false;
        return;
        }

    if(panAct == false){ 
        highlightCtrl.deactivate();
        selectedControl.deactivate();
        alert("controls deactivated!! Panning activated!!");
        panAct = true;
        return;
        }

    alert("panAct:" + panAct);                    
}

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.