Hiding custom controls

269 views
Skip to first unread message

KnutMarius

unread,
Jun 21, 2010, 3:51:43 AM6/21/10
to Google Maps JavaScript API v3
I have just started upgrading my application from Google maps v2 to
v3, and I have a question regarding the new way to handle custom map
controls:

At application start-up I first create a couple of custom controls and
attach them to the map. I then call the jQuery method $
("#myControlDiv").hide() in order to hide them (They will be activated
later). This does not seem to work properly, however. The controls are
all visible at application startup. I have put a breakpoint at the
call to hide(), and it seems that jQuery cannot find a div with that
ID at that point in time. In v2 I used map.removeControl(..) and
map.addControl(..) instead of the jquery show/hide, but as far as I
could read from the API this approach was not possible anymore.

Also, when I try to show() and hide() the controls _later_ in the
application it works fine! Does anyone know if there is a better way
of disabling/enabling custom controls, or if there is a simple fix I
can apply to the code I have now? It seems to me that the actual div-
element is created after I try to hide it, even though
document.createElement(..) is called before I run hide().

Chad Killingsworth

unread,
Jun 21, 2010, 8:22:18 AM6/21/10
to Google Maps JavaScript API v3
Controls are not added to the map (and thus the page DOM) until later
in the map initialization. If you try to hide them before they are
added, then the behavior you mention would be expected.

1) Don't add them to the map until you are ready to use them.
2) Hide them with css initially.

Chad Killingsworth

KnutMarius

unread,
Jun 21, 2010, 9:51:43 AM6/21/10
to Google Maps JavaScript API v3
Hi Chad,

Thanks for the tips! I ended up using the second solution, and setting
the CSS of the div to display=none upon creation of the control. Works
fine!

Thanks for your quick answer!

On Jun 21, 2:22 pm, Chad Killingsworth
Reply all
Reply to author
Forward
0 new messages