How to convert a LatLng and a radius to a LatLngBounds in Android Google Maps API v2?

I believe this is a limitation of the recent Google Maps API v2. They have recently added the ability to draw a Circle on the ground - but if you want to position the camera such that it shows the entire Circle, there exists no way to do so.

One can call CameraUpdateFactory#newLatLngBounds(bounds, padding) where "bounds" is a LatLngBounds and "padding" is a distance in pixels. The issue is that there is no way to create a LatLng and a radius into a LatLngBounds.

The constructor for LatLngBounds only takes 2 LatLng instances and generates a rectangle where these are the NW and SE corners.


Just like Risadinha mentioned, you can easily achieve that with android-maps-utils. Just add:

compile ''

to your gradle dependencies, use the following code:

public LatLngBounds toBounds(LatLng center, double radiusInMeters) {
    double distanceFromCenterToCorner = radiusInMeters * Math.sqrt(2.0);
    LatLng southwestCorner =
                SphericalUtil.computeOffset(center, distanceFromCenterToCorner, 225.0);
    LatLng northeastCorner = 
                SphericalUtil.computeOffset(center, distanceFromCenterToCorner, 45.0);
    return new LatLngBounds(southwestCorner, northeastCorner);


Our goal is to calculate two points (LatLngs): southwestCorner and northeastCorner. From the javadoc of the SphericalUtil you can read that 225 and 45 are heading values, and the distanceFromCenterToCorner is the distance. Further explanation of the values in the picture below:

This is totally doable.

The LatLng is the center of your circle correct? What you want to do is inscribe your circle inside of the LatLngBounds (Circle inside a Square problem), so the entire thing will show up on the map.

If you draw this on paper you can see that you have everything you need to calculate your LatLngBounds.

Remember how to find the lengths of the sides of a right triangle?

a² + b² = c²

If you draw a line from the center of your circle to the NW (upper left) corner, and another straight to the Western wall (straight line from center, to the left) of the square you have a triangle. Now you can use the equation above to solve for c since you know the the length of the other sides of the triangle (the circle's radius).

So now your equation becomes

r² + r² = c²

which reduces to

2r² = c²

which further reduces to

c = squareRoot(2) * r

Now you have the distance. This is of course an oversimplification, because the Earth is not flat. If the distances aren't huge, you could use the same equation above, but modified to project a spherical earth onto a plane:

Notice this also uses the Pythagorean theorem, same as we did above.

Next you will need to calculate your endpoints (NW, and SE corners) from your center point given a bearing, and the distance you found above.

This post may help: Calculate endpoint given distance, bearing, starting point

Don't forget to convert your degrees to radians when using the equation from the post linked above! ( Multiply degrees by pi/180 )

With the javascript library you can draw a circle with a center and radius and then get its bounds.

centerSfo = new google.maps.LatLng(37.7749295, -122.41941550000001);
circle = new google.maps.Circle({radius: 5000, center: centerSfo});
bounds = circle.getBounds();

You could do the same using the android api.

There is a utility library by Google for that:

Recommended by Google for this task and example code:

While Bartek Lipinski's answer is correct if your LatLngBounds define a square, most LatLngBounds define rectangles, and as such, the bearings of the North-East and the South-West points from the center will not always be 45 and 255 degrees.

Therefore if you are looking to get the LatLngBounds of a radius from a center for any quadrilateral, use the coordinates of your initial bounds.northeast and bounds.southwest like this (using SphericalUtils):

LatLng northEast = SphericalUtil.computeOffset(center, radius * Math.sqrt(2.0), SphericalUtil.computeHeading(center, bounds.northeast));
LatLng southWest = SphericalUtil.computeOffset(center, radius * Math.sqrt(2.0), (180 + (180 + SphericalUtil.computeHeading(center, bounds.southwest))));

(180 + (180 + x)) calculates the bearing of the South-West point from the center clockwise.

Need Your Help

windows phone 7.. play or pause the music


i am developing game in windows phone 7. i want to add a button on homepage which allow to toggle between images.

PHP fsockopen Is Slow

php networking performance imap fsockopen

I'm playing around with the IMAP protocol in PHP using fsockopen to send and receive commands. My preliminary experiments work but are insanely slow. It takes about 2 minutes for the simple function