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 'com.google.maps.android:android-maps-utils:0.4.4'

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);
}

EDIT:

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:

visual explanation


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.


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:

http://en.wikipedia.org/wiki/Geographical_distance#Flat-surface_formulae

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.

enter image description here

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 )


There is a utility library by Google for that:

http://googlemaps.github.io/android-maps-utils/

Recommended by Google for this task and example code: http://code.google.com/p/gmaps-api-issues/issues/detail?id=5704