Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialIan Svoboda
16,639 PointsGoogle Maps - Map not centering on resizing (responsive)
Hello All,
I'm working on a Google Maps implementation which is similar to the one outlined in this project. The API has changed since this video came out, but the core functionality appears to be the same.
I'm wanting the center to be preserved on window size, just like Andrew discusses in the video. However, i'm not quite able to make this happen.
Here is the script i'm using. This script is located on a PHP page just after the google maps API script tag (which includes my API key):
<script>
var locationAddress = new google.maps.LatLng(30.13422, -81.63824);
var marker;
var map1;
var center;
function initialize() {
var map1Options = {
center: locationAddress,
zoom: 15
};
map1 = new google.maps.Map(document.getElementById('map1'), map1Options);
marker = new google.maps.Marker({
map: map1,
draggable: false,
animation: google.maps.Animation.DROP,
position: locationAddress
});
}
google.maps.event.addDomListener(window, 'load', initialize);
google.maps.event.addDomListener(map1, 'idle', function() {
center = map1.getCenter();
});
$(window).resize(function() {
map1.setCenter(center);
});
</script>
I've done some searching around to confirm if the method described in the video is still valid and it absolutely is, however the above code isn't working for me. The map shows, my marker shows, the map does not retain its center position when the page is resized.
Other helpful tidbits:
- Latest version of jQuery is included in the header. This script (and the gMaps script tag mentioned above) are located in the footer area at the end of the body element.
-
My chrome console reports an error in the main.js file, which is part of the Google Maps API:
Uncaught TypeError: Cannot read property 'addEventListener' of undefined
Any direction/assistance is most appreciated. Thanks!
1 Answer
Nathan Morgan
7,957 PointsHi Ian,
I was working with the google maps api v3 a few months ago, so I saw your post and thought I could be of some help!
I think the error you're getting refers to the DOM listener 'idle' being undefined. However, if you register 'idle' via addListener instead of addDomListener by changing:
google.maps.event.addDomListener(map1, 'idle',
... etc..
to
google.maps.event.addListener(map1, 'idle',
... etc..
It should remove that error.... BUT you might get another error now:
Uncaught TypeError: Cannot read property '__e3_' of undefined
resulting from the same line but a different place:
(map1
Because of the placement of that addListener function, map1 is not yet initialized so the listener cannot be registered to it. If you instead, put the idle listener registration inside your initialize function (and anywhere after map1 is defined):
map1 = new google.maps.Map(document.getElementById('map1'), map1Options);
//
// now put: google.maps.event.addListener(map1, 'idle',... etc..
then, you should be good to go. I tested it out and it worked for me.
Let me know if this helps!
Also here are the most relevant docs from google's ref:
https://developers.google.com/maps/documentation/javascript/events#EventListeners