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

和自甴很熟 提交于 2019-11-27 17:41:56
Bartek Lipinski

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:

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.

Christopher Perry

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!