zoom_changed listener problem

269 views
Skip to first unread message

nichobot

unread,
Sep 19, 2010, 8:09:59 PM9/19/10
to Google Maps JavaScript API v3
Hi,

I am new to Google maps and javascript, and am hitting a wall while
following the documentation on events and listeners for V3.

I followed the instructions for adding a listener to recenter the map
on the city of Darwin when zoom_changed is fired by the API, as
indicated here:

http://code.google.com/apis/maps/documentation/javascript/events.html#EventListeners

My code seems fine:


var centerLatitude = 37.818361
var centerLongitude = -122.198032
var startZoom = 3

function initialize() {
var latlng = new google.maps.LatLng(centerLatitude,
centerLongitude);
var myOptions = {
zoom: startZoom,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new
google.maps.Map(document.getElementById("map_canvas"), myOptions);

google.maps.event.addListener(map, 'zoom_changed', function() {
setTimeout(moveToDarwin, 3000);
});

var marker = new google.maps.Marker({
position: latlng,
map: map,
title:"Hello World!"
});
google.maps.event.addListener(marker, 'click', function() {
map.setZoom(12);
});

}

function moveToDarwin() {
var darwin = new google.maps.LatLng(-12.461334, 130.841904);
map.setCenter(darwin);
}


The map loads fine, the marker created in the code is present and
responds to the 'click' event by zooming, as expected.

But when I zoom (either manually or via clicking the marker), I don't
get the expected behavior of the map automatically recentering to the
specified coordinates. Is it possible that 'zoom_changed' isn't being
fired? How can I debug this? Is it possible to check this with
Firebug?

Any help is appreciated. Like I said, I'm very new to this and I
realize this might seem like a dumb question, but I have been trying
to figure this out for the last day and I am at a dead end.

Thanks in advance.

nichobot

unread,
Sep 19, 2010, 8:27:48 PM9/19/10
to Google Maps JavaScript API v3
Sure enough, once I reach out for help, I find my own solution:

My issue was that I instantiated (right word?):

var map;

within the initialize function, and not globally, so it wasn't defined
for the moveToDarwin function.

var map;
function initialize() {
var latlng = new google.maps.LatLng(centerLatitude,
centerLongitude);
var myOptions = {
zoom: startZoom,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};

map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);

.....



Thanks anyway for not rejecting my noobie question.

Until next time!


On Sep 19, 5:09 pm, nichobot <nbfow...@gmail.com> wrote:
> Hi,
>
> I am new to Google maps and javascript, and am hitting a wall while
> following the documentation on events and listeners for V3.
>
> I followed the instructions for adding a listener to recenter the map
> on the city of Darwin when zoom_changed is fired by the API, as
> indicated here:
>
> http://code.google.com/apis/maps/documentation/javascript/events.html...
Reply all
Reply to author
Forward
0 new messages