LatLngBounds extend bug?

194 views
Skip to first unread message

DiTieM

unread,
Aug 19, 2011, 6:01:42 PM8/19/11
to google-map...@googlegroups.com
Just got this simple test case:

    var bounds = new google.maps.LatLngBounds( ) ;
    
    bounds.extend( new google.maps.LatLng( -34,  -56 ) ) ;
    bounds.extend( new google.maps.LatLng(  34, -118 ) ) ;
    bounds.extend( new google.maps.LatLng(  25,  121 ) ) ;

    console.log( [ bounds.getSouthWest( ).lat( )
                 , bounds.getSouthWest( ).lng( )
                 , bounds.getNorthEast( ).lat( )
                 , bounds.getNorthEast( ).lng( ) ] ) ;

I might be totally wrong, or missing something, but I would expect to get a bounding box like:
[-34, -118, 25, 121]

(The order is (SW-lat, SW-lng, NE-lat, NE-lng)

But I get this (from google maps)

[-34, 121, 34, -56]

Is this a bug or someone can explain this to me?

Thanks a lot,
  David

Ben Appleton

unread,
Aug 19, 2011, 6:40:20 PM8/19/11
to google-map...@googlegroups.com

This is because longitudes wrap around at -180 = 180 degrees. Focusing just on the longitudes:
1 - First you add the point -56
2 - Then you add the point -118. The bounds are now [-118, 56].
3 - Lastly you add the point 126. The smallest bounds containing 126 and the previous bounds is [126, -56], which crosses 180 as you observe.

Android brevity

> --
> You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/google-maps-js-api-v3/-/iLOwWNeyWQ0J.
> To post to this group, send email to google-map...@googlegroups.com.
> To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.
>

DiTieM

unread,
Aug 19, 2011, 7:04:14 PM8/19/11
to google-map...@googlegroups.com
Thanks a lot! It makes sense now. Pity I had to learn this in the painful way.

Regards,
  David
Reply all
Reply to author
Forward
0 new messages