Setting the exact bounds of a map

30,095 views
Skip to first unread message

Klaas

unread,
Jul 13, 2010, 4:30:20 PM7/13/10
to Google Maps JavaScript API v3
Hi,

I've got a simple map and I want to set it's exact bounds.

I tried using map.fitBounds(myBounds) and map.panToBounds(myBounds),
but both do not set the bounds. They just adjust the map bounds in the
way that myBounds fits into the displayed bounds.

If I call map.fitBounds( map.getBounds()), it zooms out. How can i
specify the exact bounds of a map? Something where
map.specialSettingOfBounds( map.getBounds()) does not modify the
bounds.

Any ideas?

Thanks for your help, any hints appreciated,
Klaas.

Rossko

unread,
Jul 13, 2010, 10:07:53 PM7/13/10
to Google Maps JavaScript API v3
> I've got a simple map and I want to set it's exact bounds.

You can't ; zoom is only available in integer levels, so a map of
fixed viewing pane size cannot be exactly fitted to any particular
bounds. Then there's the issue of proportions ; if your bounds were
square and the viewport rectangular an exact fit cannot be achieved.
"Square" is itself pretty meaningless here, due to looking at a curved
world's surface through the distortion of Mercator's projection.

The fitBounds method does what it can to give a map view that
_includes_ the given bounds, but usually includes a bit more too.
What you see is how it is intended to work.

If you want to change the map centre without changing the zoom, you
can.

I guess you could write some complicated code that actually resized
the map pane somewhere in between a minimum size and a maximum twice
as big, to give a good fit to bounds ; it would look a bit weird in
action though.

Klaas

unread,
Jul 14, 2010, 8:12:31 AM7/14/10
to Google Maps JavaScript API v3

On Jul 14, 4:07 am, Rossko <ros...@culzean.clara.co.uk> wrote:
> > I've got a simple map and I want to set it's exact bounds.
>
> You can't ; zoom is only available in integer levels, so a map of
> fixed viewing pane size cannot be exactly fitted to any particular
> bounds.   Then there's the issue of proportions ; if your bounds were
> square and the viewport rectangular an exact fit cannot be achieved.
> "Square" is itself pretty meaningless here, due to looking at a curved
> world's surface through the distortion of Mercator's projection.
>
> The fitBounds method does what it can to give a map view that
> _includes_ the given bounds, but usually includes a bit more too.
> What you see is how it is intended to work.

Thanks for your reply. I'm aware that the map has zoom levels and no
continuous zoom and you are right, that I can not expect it to
'accept'
all of my bounds that I specify. But what I find strange is, that
the call

map.fitBounds( map.getBounds())

changes the bounds. In my opinion the fit bounds method wouldn't have
to do anything.

I also disabled the UI with "disableDefaultUI: true" in
MapOptions, because the documentation of the other possible method
Map.panToBounds() says: "The bounds will be positioned inside the area
bounded by the map type and navigation controls, if they are present
on
the map.".



Further investigations show that even if I shrink the bounds before
applying them to the map with fitBounds() the map will move south:

Instead of a simple

map.fitBounds( map.getBounds())

I'm doing a shrinking of the bounds before reapplying them:

var bounds = map.getBounds();

var ne = bounds.getNorthEast();
var sw = bounds.getSouthWest();

var k = 5.0;

var n = ne.lat() - k;
var e = ne.lng() - k;
var s = sw.lat() + k;
var w = sw.lng() + k;

var neNew = new google.maps.LatLng( n, e );
var swNew = new google.maps.LatLng( s, w );
var boundsNew = new google.maps.LatLngBounds( swNew, neNew );

qlog( "bo=" + bounds.toUrlValue() );
qlog( "bn=" + boundsNew.toUrlValue() );

map.fitBounds( boundsNew );

If I look at the log I get:

log:bounds are: 34.931422,28.897984,57.63981,49.772008
log:bo=34.931422,28.897984,57.63981,49.772008
log:bn=39.931422,33.897984,52.63981,44.772008
log:bounds_changed_listerner: 33.916404,28.897984,56.975181,49.772008
log:bo=33.916404,28.897984,56.975181,49.772008
log:bn=38.916404,33.897984,51.975181,44.772008
log:bounds_changed_listerner: 32.914338,28.897984,56.315118,49.772008
log:bo=32.914338,28.897984,56.315118,49.772008
log:bn=37.914338,33.897984,51.315118,44.772008
log:bounds_changed_listerner: 31.92558,28.897984,55.659918,49.772008
log:bo=31.92558,28.897984,55.659918,49.772008
log:bn=36.92558,33.897984,50.659918,44.772008
log:bounds_changed_listerner: 30.950443,28.897984,55.00986,49.772008
log:bo=30.950443,28.897984,55.00986,49.772008
log:bn=35.950443,33.897984,50.00986,44.772008
log:bounds_changed_listerner: 29.989199,28.897984,54.3652,49.772008
log:bo=29.989199,28.897984,54.3652,49.772008
log:bn=34.989199,33.897984,49.3652,44.772008
log:bounds_changed_listerner: 29.042078,28.897984,53.72618,49.772008
log:bo=29.042078,28.897984,53.72618,49.772008
log:bn=34.042078,33.897984,48.72618,44.772008
log:bounds_changed_listerner: 28.109274,28.897984,53.09302,49.772008
log:bo=28.109274,28.897984,53.09302,49.772008
log:bn=33.109274,33.897984,48.09302,44.772008
log:bounds_changed_listerner: 27.190946,28.897984,52.465925,49.772008
log:bo=27.190946,28.897984,52.465925,49.772008
log:bn=32.190946,33.897984,47.465925,44.772008
log:bounds_changed_listerner: 26.287218,28.897984,51.845083,49.772008

As you can see, the east and west coordinates stay the same, but the
north
and south coordinates move slowly south.


I guess, there's not a lot that one can do... but if anyone has
another idea? :-)

Best regards,
Klaas.

Rossko

unread,
Jul 14, 2010, 9:17:07 AM7/14/10
to Google Maps JavaScript API v3
> But what I find strange is, that
> the call
> map.fitBounds( map.getBounds())
> changes the bounds. In my opinion the fit bounds method wouldn't have
> to do anything.

fitBounds will try to present a map that CONTAINS the bounds you give
it. Imagine if you had a drawn polygon that size ; if the map fitted
to it perfectly it would not be visible (at the border of the
viewport). I'm sure this is working as intended.

> I also disabled the UI with "disableDefaultUI: true" in
> MapOptions, because the documentation of the other possible method
> Map.panToBounds() says: "The bounds will be positioned inside the area
> bounded by the map type and navigation controls, if they are present
> on
> the map.".

I would imagine (but haven't checked) that fitBounds also takes into
account the logo and copyright at the bottom and tries to avoid those,
even if you have removed all other controls.

There doesn't seem to be any reason that you couldn't write your own
fitBoundsSpecial() if you don't like the existing behaviour. The
"real" bounds of the map are available to you, as are methods to
change the zoom and centre, and as you seem to want to ignore any
controls placed on the map you needn't worry about finding out where
they are placed. I don't know of anyone who has done that already.

Klaasmer

unread,
Jul 14, 2010, 9:41:53 AM7/14/10
to google-map...@googlegroups.com
> I would imagine (but haven't checked) that fitBounds also takes into
> account the logo and copyright at the bottom and tries to avoid those,
> even if you have removed all other controls.

Yes, you are right. The 'moving south' in my test case is about the distance
that the logo and copyright require.

> There doesn't seem to be any reason that you couldn't write your own
> fitBoundsSpecial() if you don't like the existing behaviour. The
> "real" bounds of the map are available to you, as are methods to
> change the zoom and centre, and as you seem to want to ignore any
> controls placed on the map you needn't worry about finding out where
> they are placed. I don't know of anyone who has done that already.

Yes, I'm going to write my own version. The hard part will be how to figure
out the correct zoom in one step.

Thanks again!


Reply all
Reply to author
Forward
0 new messages