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.
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
andbounds.southwest
like this (using SphericalUtils):(180 + (180 + x))
calculates the bearing of the South-West point from the center clockwise.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?
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
which reduces to
which further reduces to
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.
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.
You could do the same using the android api.
Just like Risadinha mentioned, you can easily achieve that with
android-maps-utils
. Just add:to your gradle dependencies, use the following code:
EDIT:
Our goal is to calculate two points (
LatLngs
):southwestCorner
andnortheastCorner
. From the javadoc of theSphericalUtil
you can read that225
and45
areheading
values, and thedistanceFromCenterToCorner
is thedistance
. Further explanation of the values in the picture below: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