awa*_*oor 18 android location google-maps
有没有办法从中间点找到可见地图的半径?

我想从API获取靠近地图中心点的近处,并且该API需要lat,lng和radius.我能够从中心点获得lat和lng,但无法找到获得半径的方法.
谢谢
kah*_*aho 20
对于Google Maps Android API,您可以通过...获取界限
从地图引用中,获取Projectionby getProjection().和,
投影用于在屏幕位置和地理坐标之间进行转换.
因此,从投影中,我们可以使用getVisibleRegion(),并获取地图的VisibleRegion,其中包含LatLngBounds,这是一个包含2个LatLng变量的类,一个用于边界的东北角,另一个用于西南角. .
所以代码应该是这样的:
googleMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition position) {
LatLngBounds bounds = googleMap.getProjection().getVisibleRegion().latLngBounds;
LatLng northeast = bounds.northeast;
LatLng southwest = bounds.southwest;
Context context = getApplicationContext();
CharSequence text = "ne:"+northeast+" sw:"+southwest;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
});
Run Code Online (Sandbox Code Playgroud)
= - = - = - = - = - =编辑:
可能是我太天真,只考虑NE和SW可以解决这个问题,但只有在用户没有旋转地图或向上倾斜3D地图的特殊情况下.
所以相反,你可以抓住VisibleRegion,提供4个变量,farRight,farLeft,nearRight,nearLeft,每个代表该区域的4个连接器.
然后我们可以计算该4个点的区域的宽度和高度,并选择较小的一个(好吧,有时候宽度可能比我猜的高度大.)
为了计算,我们可以使用Location.distanceBetween(x1,y1,x2,y2,result)函数...
这使得代码如下所示:
VisibleRegion visibleRegion = googleMap.getProjection().getVisibleRegion();
LatLng farRight = visibleRegion.farRight;
LatLng farLeft = visibleRegion.farLeft;
LatLng nearRight = visibleRegion.nearRight;
LatLng nearLeft = visibleRegion.nearLeft;
float[] distanceWidth = new float[2];
Location.distanceBetween(
(farRight.latitude+nearRight.latitude)/2,
(farRight.longitude+nearRight.longitude)/2,
(farLeft.latitude+nearLeft.latitude)/2,
(farLeft.longitude+nearLeft.longitude)/2,
distanceWidth
);
float[] distanceHeight = new float[2];
Location.distanceBetween(
(farRight.latitude+nearRight.latitude)/2,
(farRight.longitude+nearRight.longitude)/2,
(farLeft.latitude+nearLeft.latitude)/2,
(farLeft.longitude+nearLeft.longitude)/2,
distanceHeight
);
float distance;
if (distanceWidth[0]>distanceHeight[0]){
distance = distanceWidth[0];
} else {
distance = distanceHeight[0];
}
Run Code Online (Sandbox Code Playgroud)
Thi*_*nce 10
非常感谢你的回答@kaho,它帮了我很多(甚至你以同样的方式计算了distanceWidth和distanceHeight).
澄清:
farLeft LatLng对象,用于定义摄像机的左上角.
farRight LatLng对象,用于定义摄像机的右上角.
nearLeft LatLng对象,用于定义摄像机的左下角.
nearRight LatLng对象,用于定义摄像机的右下角.
编辑:我不知道为什么我们做一个简单的计算变得有点复杂,可见半径只是一个可见的对角线,这就是全部!
private double getMapVisibleRadius() {
VisibleRegion visibleRegion = map.getProjection().getVisibleRegion();
float[] diagonalDistance = new float[1];
LatLng farLeft = visibleRegion.farLeft;
LatLng nearRight = visibleRegion.nearRight;
Location.distanceBetween(
farLeft.latitude,
farLeft.longitude,
nearRight.latitude,
nearRight.longitude,
diagonalDistance
);
return diagonalDistance[0] / 2;
}
Run Code Online (Sandbox Code Playgroud)
我还记录了我的结果,以便与@ jossef-harush的结果进行比较,它大约是:
| 归档时间: |
|
| 查看次数: |
5873 次 |
| 最近记录: |