Listening for map resize event...

3,050 views
Skip to first unread message

Kurteknikk

unread,
Jun 10, 2010, 4:44:54 AM6/10/10
to Google Maps JavaScript API v3
Hi friends,

I'm using google maps api v3 and i'm trying to listen for the resize
event but i just can't get it working. Is this code correct ? Btw
mapCanvas is the real div's name so that is 100% correct. And map
obviously is a public variable containing the current map.

This is the code i'm talking about:

//Trigger the resize event when map's div is resized
var mapDiv = document.getElementById('mapCanvas');
google.maps.event.addDomListener(mapDiv, 'resize', function(){
alert('div resize');
google.maps.event.trigger(map,'resize');
});

google.maps.event.addListener(map, "resize", function() {
alert('resize');
});

Pil

unread,
Jun 10, 2010, 9:41:50 AM6/10/10
to Google Maps JavaScript API v3
What you're trying to do will definitely freeze the browser because
there will be too many alerts. Only one such event handler will freeze
the browser, but you're registering two of them

Try to use Firebug or to use a div as resize reporter


google.maps.event.addListener(map, "resize", function() {
document.getElementById("report").innerHTML = 'map resized';
});

Chad Killingsworth

unread,
Jun 10, 2010, 12:48:11 PM6/10/10
to Google Maps JavaScript API v3
Instead of using alerts, I suggest using "console.log(message)" - it
works with the development tools in all major browsers. Use firebug
for FireFox.

Chad Killingsworth

Susannah (Google Employee)

unread,
Jun 10, 2010, 9:10:37 PM6/10/10
to Google Maps JavaScript API v3
Divs do not have a resize event - only the window does.

-Susannah

Kurt Agius

unread,
Jun 11, 2010, 8:36:47 AM6/11/10
to google-map...@googlegroups.com
Cheers susannah & all the others who tried to help.
Infact i just solved it by detecting the resize event of the window and then triggering the resize event of my map, cheers :)

--
You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
To post to this group, send email to google-map...@googlegroups.com.
To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.


Chad Killingsworth

unread,
Jun 11, 2010, 3:05:27 PM6/11/10
to Google Maps JavaScript API v3
That should have been the default behavior . . . you shouldn't have
had to do that.

Chad Killingsworth
> > google-maps-js-a...@googlegroups.com<google-maps-js-api-v3%2B unsub...@googlegroups.com>
> > .

Kurt Agius

unread,
Jun 15, 2010, 6:00:56 AM6/15/10
to google-map...@googlegroups.com
Hi Chad,

Apparently its not because i just need to do it... I'm using firefox 3.6.3 and i cause the majority of developers are using that version so maybe it should be double checked...

To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.

Kurt Agius

unread,
Jun 15, 2010, 6:16:00 AM6/15/10
to google-map...@googlegroups.com
Btw cause i forgot to mention it, the documentation claims that the developer should trigger the map resize manually.

"Developers should trigger this event on the map when the div changes size: google.maps.event.trigger(map, 'resize')"

Chad Killingsworth

unread,
Jun 15, 2010, 9:37:48 AM6/15/10
to Google Maps JavaScript API v3
Yes - if you change the size (or visibility) of the div through
JavaScript you must trigger that event. However, the API automatically
listens to the window resize event - that's how the 100% height/width
samples work. You don't manually need to trigger on that one.

If you post a link to your site, we can take a look at the issue.

Chad Killingsworth

On Jun 15, 5:16 am, Kurt Agius <kurtekn...@gmail.com> wrote:
> Btw cause i forgot to mention it, the documentation claims that the
> developer should trigger the map resize manually.
>
> "Developers should trigger this event on the map when the div changes size:
> google.maps.event.trigger<http://code.google.com/apis/maps/documentation/javascript/reference.h...>(map,
> >> > > google-maps-js-a...@googlegroups.com<google-maps-js-api-v3%2B unsub...@googlegroups.com><google-maps-js-api-v3%2B
Reply all
Reply to author
Forward
0 new messages