Setting Global Variable [Zoom Level] in JS on Event Trigger

102 views
Skip to first unread message

kodemonki

unread,
Dec 12, 2010, 10:26:33 AM12/12/10
to google-map...@googlegroups.com

I'm using Google Maps API v3 and need to offset markers based on the current zoom level.

I have a global variable current_zoom_level, and an event listener for zoom in/out:

google.maps.event.addListener(map, 'zoom_changed', function() { current_zoom_level = map.getZoom(); alert(current_zoom_level); });

I've tried this bit in the initialization code and the code that calculates the offset. The alert displays the correct current zoom level, but when I access it in build_loan_markers() (where I calculate offsets) current_zoom_level is undefined.

function build_loan_markers () {
var offset; alert(current_zoom_level); if(!window.current_zoom_level){ window.current_zoom_level = 1; } offset = 1/(window.current_zoom_level^2); alert('zoom: ' + current_zoom_level + ' offset: ' + offset); etc.. }

How can I globally set this variable?

Thank you!

Esa

unread,
Dec 12, 2010, 10:50:47 AM12/12/10
to Google Maps JavaScript API v3
Please provide a link to your page so that we can see how you tried to
define the variable.

Did you define it by 'var' in global scope?

Rossko

unread,
Dec 12, 2010, 4:06:28 PM12/12/10
to Google Maps JavaScript API v3
> I'm using Google Maps API v3 and need to offset markers based on the
> current zoom level.

It may be that you just need to set the anchor of your marker imagery
properly, instead of applying offsets?

kodemonki

unread,
Dec 12, 2010, 10:26:01 PM12/12/10
to Google Maps JavaScript API v3
I did define the global variable by var in the global scope. We
needed to offset the markers because four markers on top of each other
(representing different things) would be useless.

The reason the global var didn't look like it was getting set was
because the function that needed it was only ever called once, not
each time the page was zoomed in/out on.

Thank you!
Reply all
Reply to author
Forward
0 new messages