如何在经纬度和半径转换为的LatLngBounds在Android的谷歌地图API第2版? [英] How to convert a LatLng and a radius to a LatLngBounds in Android Google Maps API v2?
问题描述
我相信这是近期谷歌地图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.
可调用CameraUpdateFactory#newLatLngBounds(边界,填充)其中范围是的LatLngBounds和填充是以像素为单位的距离。的问题是,没有办法来创建一个经纬度和一个半径成的LatLngBounds
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.
该构造的LatLngBounds只需要2经纬度实例,并生成一个矩形,其中这些是西北和东南角落。
The constructor for LatLngBounds only takes 2 LatLng instances and generates a rectangle where these are the NW and SE corners.
推荐答案
就像 Risadinha 提到的,你可以很容易地实现与 Android的地图-utils的
。只需添加:
Just like Risadinha mentioned, you can easily achieve that with android-maps-utils
. Just add:
compile 'com.google.maps.android:android-maps-utils:0.3.4'
你的摇篮依赖关系,并使用以下code:
to your gradle dependencies, and use the following code:
public LatLngBounds convertCenterAndRadiusToBounds(LatLng center, double radius) {
LatLng southwest = SphericalUtil.computeOffset(center, radius * Math.sqrt(2.0), 225);
LatLng northeast = SphericalUtil.computeOffset(center, radius * Math.sqrt(2.0), 45);
return new LatLngBounds(southwest, northeast);
}
编辑:
我们的目标是计算两个点(<$ C C $> LatLngs ):西南
和东北
。从的<一的Javadoc href="http://googlemaps.github.io/android-maps-utils/javadoc/com/google/maps/android/SphericalUtil.html#computeOffset-LatLng-double-double-"><$c$c>SphericalUtil$c$c>你可以阅读 225
和 45
是标题
值,和半径*的Math.sqrt(2.0)
是距离
。
Our goal is to calculate two points (LatLngs
): southwest
and northeast
. From the javadoc of the SphericalUtil
you can read that 225
and 45
are heading
values, and the radius * Math.sqrt(2.0)
is the distance
. Further explanation of the values in the picture below:
这篇关于如何在经纬度和半径转换为的LatLngBounds在Android的谷歌地图API第2版?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!