request for function: construct LatLngBounds from LatLng and radius

1,303 views
Skip to first unread message

fearless_fool

unread,
Jan 30, 2012, 12:41:37 PM1/30/12
to google-map...@googlegroups.com
I haven't found a straightforward way to construct a LatLngBounds given a center point and a radius.  The best approaches I've seen are described in http://www.movable-type.co.uk/scripts/latlong.html, but it seems odd that I'd need to put that much trig code in my own javascript code -- it ought to be a library function.

If there is such such a built-in function, I haven't found it.  If there isn't, I'd like to see it added.

Andrew Leach

unread,
Jan 30, 2012, 12:48:04 PM1/30/12
to google-map...@googlegroups.com
On 30 January 2012 17:41, fearless_fool <rdp...@gmail.com> wrote:
> I haven't found a straightforward way to construct a LatLngBounds given a
> center point and a radius.  The best approaches I've seen are described
> in http://www.movable-type.co.uk/scripts/latlong.html, but it seems odd that
> I'd need to put that much trig code in my own javascript code -- it ought to
> be a library function.

Geometry library
http://code.google.com/apis/maps/documentation/javascript/geometry.html#Distance

"Given a particular heading, an origin location, and the distance to
travel (in meters), you can calculate the destination coordinates
using computeOffset()."

You can construct a LatLngBounds by using four calls to computeOffset().

Rossko

unread,
Jan 30, 2012, 12:50:41 PM1/30/12
to Google Maps JavaScript API v3
> If there is such such a built-in function, I haven't found it.  If there
> isn't, I'd like to see it added.

Isn't it Circle() ?http://code.google.com/apis/maps/documentation/
javascript/overlays.html#Circles
which has a getBounds() method
http://code.google.com/apis/maps/documentation/javascript/reference.html#Circle

fearless_fool

unread,
Jan 30, 2012, 5:51:05 PM1/30/12
to google-map...@googlegroups.com
@Rossko:

Yes, it appears that something like:

function convertToBounds(latLng, radius) {
  return new google.maps.Circle({center: latLng, radius: radius}).getBounds();
}

... should do the trick. Thank you!

DiTieM

unread,
Feb 7, 2012, 5:46:06 AM2/7/12
to google-map...@googlegroups.com
It took time to find that function to me (I dont remember the source). It could certainly be added to the API set.

function llr_to_bounds ( lat, lng, distance )
{
  // angular distance in radians on a great circle
  var radius  = 6371000
    , radDist = distance / radius
    , radLat  = to_rad( lat )
    , radLon  = to_rad( lng )
    , minLat  = radLat - radDist
    , maxLat  = radLat + radDist
    , minLon
    , maxLon
    , MIN_LAT = -Math.PI/2
    , MAX_LAT =  Math.PI/2
    , MIN_LON = -Math.PI
    , MAX_LON =  Math.PI   ;

  if ( minLat > MIN_LAT && maxLat < MAX_LAT )
    {
       var deltaLon = Math.asin( Math.sin( radDist ) / Math.cos( radLat ) ) ;
        
       minLon = radLon - deltaLon;
       maxLon = radLon + deltaLon;

       if ( minLon < MIN_LON ) minLon += 2 * Math.PI ;
       if ( maxLon > MAX_LON ) maxLon -= 2 * Math.PI ;
    } 
  else
    {
// a pole is within the distance
minLat = Math.max( minLat, MIN_LAT ) ;
maxLat = Math.min( maxLat, MAX_LAT ) ;
minLon = MIN_LON ;
maxLon = MAX_LON ;
    }

  return new google.maps.LatLngBounds( new google.maps.LatLng( to_deg( minLat ), to_deg( minLon ) )
                                     , new google.maps.LatLng( to_deg( maxLat ), to_deg( maxLon ) ) ) ;
}

Rossko

unread,
Feb 7, 2012, 6:12:18 AM2/7/12
to Google Maps JavaScript API v3
> It took time to find that function to me (I dont remember the source). It
> could certainly be added to the API set.

Why? Circle() exists already and has a getBounds() method
Is there a problem with that? (might not work well at the poles for
example, not much does in Mercator)

JKurtock

unread,
Feb 8, 2012, 12:32:57 AM2/8/12
to Google Maps JavaScript API v3
Remember that everything added to an API set makes the API larger,
slower, harder to load, etc. If only 1% of users want to use a
function, and it's a matter of 20 lines of javascript for those users
to add it themselves, why would you want to burden the other 99%?

The development team does (I think) a very good job of adding that
which is truly useful, and leaving out the "nice-to-haves." So don't
feel bad if your pet function is not picked up.

- Jeff

DiTieM

unread,
Feb 8, 2012, 4:04:47 AM2/8/12
to google-map...@googlegroups.com
hmmm Rosko, now I understood your post. Need to check that function! 
But looks like the correct answer, hence there is absolutely no need for 
the function I posted!

Sorry for the mess
Reply all
Reply to author
Forward
0 new messages