struggling with getBounds()

688 views
Skip to first unread message

Mark1000

unread,
Jul 16, 2010, 6:02:33 PM7/16/10
to Google Maps JavaScript API v3
Hey, new experimenter here.. and I am struggling with the ability to
get the size of my map box. Eventually I plan to interrogate my db to
see what markers to display as the user scrolls around. For now I am
just trying to get the bounds of the map. I suspect it is something
really basic but my brain is fried.

All the examples I can find are v2 but the v3 ref for the function
looks like it is unchanged so I am using the same syntax. the code is
below. I am receiving the "bounds is undefined" error as soon as I try
to use the bounds variable.

For convenience I have the page online at: http://www.markbsplace.net/test/test.htm


function initialize() {
var latlng = new google.maps.LatLng(40.69847032728747,
-73.9514422416687);
var mapOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("Map"),
mapOptions);

bounds = map.getBounds();
southWest = bounds.getSouthWest();
northEast = bounds.getNorthEast();
lngSpan = northEast.lng() - southWest.lng();
latSpan = northEast.lat() - southWest.lat();
}

any help or pointer to v3 example would be greatly appreciated!
Mark

geoco...@gmail.com

unread,
Jul 17, 2010, 7:14:13 AM7/17/10
to Google Maps JavaScript API v3
It is good practice to declare your variables (using the "var"
keyword). If you don't, Microsoft's browser will automatically assume
that you are refering to the div of the same name if there is one, and
IE doesn't consider case differences (so the Map div and the map
variable are the same object, unless you declare "map" as a javascript
variable with the "var" keyword)..

You can't access the map object until it exists. Your code creates
the map variable in the document onload event after the
document.writes have happened. You can't use document.write to modify
the DOM after the onload event is fired. Try creating a div and
adding contents to it with .innerHTML.

It also looks like getBounds is not available immediately:
http://groups.google.com/group/google-maps-js-api-v3/browse_frm/thread/563ed63c2bd60749/2fd92dd62b833afd?lnk=gst&q=getBounds#2fd92dd62b833afd
from this search:
http://groups.google.com/group/google-maps-js-api-v3/search?group=google-maps-js-api-v3&q=getBounds&qt_g=Search+this+group

This seems to work:
http://www.geocodezip.com/markbsplace_net_testC.html

-- Larry

> Mark

Mark1000

unread,
Jul 18, 2010, 5:43:56 PM7/18/10
to Google Maps JavaScript API v3
Thanks so much for the sample page!

An example is worth a thousand words..

Mark
Reply all
Reply to author
Forward
0 new messages