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

AndroidGoogle Maps-Android-Api-2

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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.

Solution 3 - Android

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

+= 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: https://stackoverflow.com/questions/3225803/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 )

Solution 4 - Android

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

Solution 5 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWarlaxView Question on Stackoverflow
Solution 1 - AndroidBartek LipinskiView Answer on Stackoverflow
Solution 2 - AndroidascoppaView Answer on Stackoverflow
Solution 3 - AndroidChristopher PerryView Answer on Stackoverflow
Solution 4 - AndroidRisadinhaView Answer on Stackoverflow
Solution 5 - AndroidIan WambaiView Answer on Stackoverflow