Differences between fitBounds and getBounds

1,102 views
Skip to first unread message

Web-ID

unread,
May 17, 2011, 12:04:23 PM5/17/11
to google-map...@googlegroups.com
Hi!

I have a problem with the two methods fitBounds and getBounds ...

Look at this example :

var map = new google.maps.Map(document.getElementById('map'), {
            zoom: 10,
            mapTypeId: google.maps.MapTypeId.ROADMAP
});

var ne = new google.maps.LatLng(49.018140928715304, 2.5789534085937476);
var sw = new google.maps.LatLng(48.694669918528504, 2.1230207914062476);
var latLngBounds = new google.maps.LatLngBounds(sw, ne);
map.fitBounds (latLngBounds);

google.maps.event.addListener(map, 'bounds_changed', function() {
            alert (map.getBounds().getNorthEast().lat());
// return 49.17909301188814
});

Why my North East Latitude has no exactly the same value than the alert ?

Thank's for your comments ...


geoco...@gmail.com

unread,
May 17, 2011, 12:18:50 PM5/17/11
to Google Maps JavaScript API v3
Why should it be?

fitBounds is only supposed to make viewport on the map show the
argument bounds (so the viewport can be bigger than the argument
bounds, but cannot be smaller).

map.getBounds returns the actual bounds of the viewport. There is
nothing that I know of that says they have to be the same.

-- Larry

Web-ID

unread,
May 17, 2011, 12:33:54 PM5/17/11
to google-map...@googlegroups.com
So, I use a bad method ....

How can I fix bounds and no the coordinates of the viewport ?

Thanks for your response Larry !

geoco...@gmail.com

unread,
May 17, 2011, 1:08:14 PM5/17/11
to Google Maps JavaScript API v3
On May 17, 9:33 am, Web-ID <cedric.we...@gmail.com> wrote:
> So, I use a bad method ....
>
> How can I fix bounds and no the coordinates of the viewport ?

The bounds are quanized by the zoom level (which is an integer zoom
level 2 is 2x zoom level 1). You can't necessarily get precisely the
bounds you want. What are you trying to do?

-- Larry

Chris Broadfoot

unread,
May 18, 2011, 1:33:33 AM5/18/11
to google-map...@googlegroups.com

Web-ID

unread,
May 18, 2011, 2:51:37 AM5/18/11
to google-map...@googlegroups.com
I'm trying to create an URL with the coordinates of my map in order to create an unique link to share ...

So, I create this link with getBounds which I put in my URL.
I get these coordinates when a user put this URL in his browser.

I saw that LatLngBounds class contains a "toUrlValue" method, but how can I get the values and move the map with the coordinates in parameters ?

Thanks for your response, Larry.

Rossko

unread,
May 18, 2011, 4:31:38 AM5/18/11
to Google Maps JavaScript API v3
> I saw that LatLngBounds class contains a "toUrlValue" method, but how can I
> get the values and move the map with the coordinates in parameters ?

Write some code that examines the querystring (not a maps function)
http://www.google.com/search?q=javascript+querystring

Then use the numbers you have found to fitBounds your map.

You'll probably want a default action in case there are no parameters

Web-ID

unread,
May 18, 2011, 5:59:57 AM5/18/11
to google-map...@googlegroups.com
Parsing URL in order to obtain values is not difficult ...

I'd just want to export coordinates from a map (by getBounds() of Map Class) and import these coordinates in another map of another page.

In my script I create my URL from my first map, but in my second map the viewport is no exactly the same because of differences between getBounds and fitBounds as explain by Larry ...

So I'm searching a method which can display coordinates exported by the getBounds() method.

Thanks for your comments !

ps : I hope my English is not so bad ...

geoco...@gmail.com

unread,
May 18, 2011, 8:32:38 AM5/18/11
to Google Maps JavaScript API v3
On May 18, 2:59 am, Web-ID <cedric.we...@gmail.com> wrote:
> Parsing URL in order to obtain values is not difficult ...
>
> I'd just want to export coordinates from a map (by getBounds() of
> Map Class) and import these coordinates in another map of another
> page.
>
> In my script I create my URL from my first map, but in my second
> map the viewport is no exactly the same because of differences
> between getBounds and fitBounds as explain by Larry ...
>
> So I'm searching a method which can display coordinates exported by
> the getBounds() method.

I use map.getCenter() and map.getZoom():
http://www.geocodezip.com/v3_MW_example_linktothis.html?lat=42.359212&lng=-71.093363&zoom=19&type=h

-- Larry
Reply all
Reply to author
Forward
0 new messages