[OpenLayers-Users] How to handle controls
Fernando Norte
fernando at globalgeo.com.br
Thu Mar 29 14:04:04 EDT 2012
Yes Spencer,
All your controls is part of the main Map Object.
If you analize that in Firebug you will see this controls, perhaps,
at map.controls as an array. Which one of this itens in the array is an
Object Control, with a lot of atributes.
What you can do is list through a FOR an check the name, to assign the
Control Object to an variable, that you'll used it in the
map.removeControl().
Let me put a piece of code I have to do something like this:
var drawButton; // I will use this variable to assign my control
var mapCnt = map.controls // Just to avoid repetition in the code
for (i=0; i<mapCnt.length; i++) { // List all controls there I have
if (mapCnt[i].displayClass == 'olEditorControlDrawPolygon') //
Check the displayClass there I want.. or other attribute there you
recognize.
drawButton = mapCnt[i]; // and Assigned the Control Object to
my variable...
}
map.removeControl(drawButton); //using it
Goog luck,
2012/3/29 Spencer Renosis <renosis at gmail.com>
> Fernando,
>
> Thanks for your reply. I am aware of firebug and how to find out which
> variable I want that way. I am trying to figure out the best way to do this
> while writing my application. So say I had an option screen I created on
> the application I am designing, that allows you to turn off certain
> controls. I could easily add the control, but when I want to remove the
> control, how do I specify the control I want to remove?
>
> So, user clicks button, button calls map.addControl(new
> OpenLayers.Control.ScaleLine()); and viola! the user now gets to see the
> scale line...
>
> but what happens when the user wants to get rid of the scale line.... he
> clicks the button to remove it and it calls map.removeControl(????????).
>
> The button obviously wouldn't know which control I wanted to remove
> because it could be any index number in the control array....
>
> This means my only option would be to assign it to some variable outside
> of any function, making it a global variable. Is this my only option is
> there some other way to do it?
>
> Thanks,
>
> Spencer
>
> On Thu, Mar 29, 2012 at 1:02 PM, Fernando Norte <fernando at globalgeo.com.br
> > wrote:
>
>> Hi Spencer,
>>
>> As you are a "non programmer" that maybe confused you a little bit.
>> There is two ways to not depend on array index:
>> Or you create a global variable, and it's easy, just declare (var
>> variable_name) outside any function.
>> Or you can find the specific control you want and put in a variable,
>> global or not.
>> As an array, you can check all itens to find which one have the name you
>> want.
>> It's better to find the name you want using Firebug Console.
>>
>> If you don't know how to use a Firebug Console, or search an array and
>> compare values, try learn this before start programming.
>>
>> Good Luck,
>>
>>
>>
>>
>> 2012/3/29 Spencer Renosis <renosis at gmail.com>
>>
>>> I am working with the OpenLayers 2.10 Beginner's Guide by E. Hazzard.
>>>
>>> In chapter 6 he recommends when creating controls, to reference them
>>> by variable. That way when you want to remove a control, you can
>>> specify the variable instead of the array index number, as specifying
>>> the index number can get confusing.
>>>
>>> Quote: "Another downside to using the controls array itself is that
>>> removing a control will affect
>>> the index of all other controls, so there can be a lot of guesswork
>>> involved as to what control
>>> you're actually removing. So, in short, avoid removing controls this
>>> way if you can."
>>>
>>> But, when you create a new control and assign it to an easily
>>> referenced variable, the removeControl() method won't recognize this
>>> variable unless it is a Global Variable. Now, I am not an experienced
>>> programmer by any means, but I am just curious if there is a way to do
>>> this without using a global variable. From what I understand, good
>>> design practice is to use global variable sparingly. Does this not
>>> matter that much? Am I over-thinking it? How would a more experienced
>>> programmer do this?
>>>
>>> Thanks,
>>> --
>>> Spencer Renosis
>>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20120329/3dc65271/attachment.html
More information about the Users
mailing list