有可能从中点找到可见地图的半径吗?
我想从API到地图的中心点附近的位置,该API需要lat,lng和radius.我可以从中心点得到纬度和距离,但是找不到半径的方法.
谢谢
解决方法
对于Google Maps
Android API,您可以通过…获得边界
从地图参考,获得getProjection()的投影.而且,
a projection is used to translate between on screen location and geographic coordinates..
所以从投影中我们可以使用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();
}
});
= – = – = – = – = – =
编辑:
可能我太天真了,只给NE和SW可以解决这个问题,但是只有在用户没有旋转地图或向3D倾斜的特殊情况下才可以.
所以你可以抓住VisibleRegion,它提供了4个变量,farRight,farLeft,nearRight,nearLeft,每个代表4个区域的conners.
然后我们可以计算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,distanceHeight
);
float distance;
if (distanceWidth[0]>distanceHeight[0]){
distance = distanceWidth[0];
} else {
distance = distanceHeight[0];
}