map.fitBounds

1,017 views
Skip to first unread message

theproductguy

unread,
Jun 10, 2009, 1:28:14 AM6/10/09
to Google Maps JavaScript API v3
I have been working w/ v3 for a bit now and am unable to seem to get
map.fitBounds to work. As anyone had any success with it? Can you
share an example of your code? I assume fitBounds will zoom the map
to the boundary level specified. Please also let me know if my
assumption is incorrect.

String

unread,
Jun 10, 2009, 7:53:47 AM6/10/09
to Google Maps JavaScript API v3
It works fine for me; I've built it into geoXML3, which you can find
at http://code.google.com/p/geoxml3/source/browse/trunk/geoxml3.js.
The actual fitBounds call is on line 227, but it's really simple,
essentially just:

map.fitBounds(bounds);

If that kind of syntax isn't working for you, I'd expect you have
something wrong external to the fitBounds call itself. Initial guesses
are that either 'map' or 'bounds' aren't in scope, or that your
'bounds' hasn't been initialized properly.

As usual, a link to your code would really help take the guesswork out
of this.

String

theproductguy

unread,
Jun 10, 2009, 8:16:33 AM6/10/09
to Google Maps JavaScript API v3
I've checked the values within the bounds variable during & after the
looping, and they look right. And the markers that I am setting in
the same loop are being correctly placed. The only thing that does
not seem to be working is the fitBounds function, setting the zoom
level to show all the markers.

The code looks like:

var myOptions = {
zoom:10,
center: new google.maps.LatLng(-34.397, 150.644),
scaleControl: true,
navigationControl: false,
mapTypeControl: false,
mapTypeId: google.maps.MapTypeId.HYBRID
};
var map = new google.maps.Map($("#map_result_map").get(0),
myOptions);
var map_bounds = new google.maps.LatLngBounds();

jQuery.each(items, function(i, row) {
var item_id = row[0];
var item_name = row[1];
var item_addr_lat = row[5];
var item_addr_lng = row[6];

if (item_addr_lat != null) {

var current_marker_location = new google.maps.LatLng(item_addr_lat,
item_addr_lng);

var marker = new google.maps.Marker({
position: current_marker_location,
map: map,
title: item_name
});

map_bounds.extend(current_marker_location);
}
});

map.set_center( map_bounds.getCenter( ));
map.fitBounds(map_bounds);


On Jun 10, 7:53 am, String <sterling.ud...@googlemail.com> wrote:
> It works fine for me; I've built it into geoXML3, which you can find
> athttp://code.google.com/p/geoxml3/source/browse/trunk/geoxml3.js.

String

unread,
Jun 10, 2009, 10:48:32 AM6/10/09
to Google Maps JavaScript API v3

theproductguy

unread,
Jun 10, 2009, 4:36:50 PM6/10/09
to Google Maps JavaScript API v3
Unfortunately I can provide the link to the private server, but I did
identify the issue. It turns out it was more a fluke that I was at
any time seeing normal looking maps. It looks like it was race
conditions that was resulting in my experiencing any semblance of
functionality. ;) I realized that the problem may be due to rendering
issues often associated hidden layers. Most of the time fitBounds was
run was just before (or just after) the layer within which was
contained was changed from HIDDEN to VISIBLE. Everything, from
rendering to fitBounds, is working just fine now.

Just wanted to share in case anyone else runs into this 'oops' in the
future.

On Jun 10, 10:48 am, String <sterling.ud...@googlemail.com> wrote:
> Sigh. Please post a link instead of inline code.
>
> http://groups.google.com/group/Google-Maps-API/web/suggested-posting-...
Reply all
Reply to author
Forward
0 new messages