【发布时间】:2013-12-06 11:26:20
【问题描述】:
我想用Google Maps 在地图上画一个静态圆圈。当用户捏合时,地图将放大/缩小。
我需要知道地图半径(与圆圈中包含的区域有关)并相应地更改底部的搜索栏。
有人知道如何检索从左到右屏幕边缘的距离的解决方案吗?我在Google Maps API doc 没有找到任何东西。
类似这样的:
【问题讨论】:
标签: android google-maps google-maps-android-api-2 distance
我想用Google Maps 在地图上画一个静态圆圈。当用户捏合时,地图将放大/缩小。
我需要知道地图半径(与圆圈中包含的区域有关)并相应地更改底部的搜索栏。
有人知道如何检索从左到右屏幕边缘的距离的解决方案吗?我在Google Maps API doc 没有找到任何东西。
类似这样的:
【问题讨论】:
标签: android google-maps google-maps-android-api-2 distance
使用 VisibleRegion 可以获得所有角坐标以及中心。
VisibleRegion vr = mMap.getProjection().getVisibleRegion();
double left = vr.latLngBounds.southwest.longitude;
double top = vr.latLngBounds.northeast.latitude;
double right = vr.latLngBounds.northeast.longitude;
double bottom = vr.latLngBounds.southwest.latitude;
你可以通过这个计算两个区域的距离
Location MiddleLeftCornerLocation;//(center's latitude,vr.latLngBounds.southwest.longitude)
Location center=new Location("center");
center.setLatitude( vr.latLngBounds.getCenter().latitude);
center.setLongitude( vr.latLngBounds.getCenter().longitude);
float dis = center.distanceTo(MiddleLeftCornerLocation);//calculate distane between middleLeftcorner and center
【讨论】:
我有时间在@Md 给出的出色答案中编写“MiddleLeftCornerLocation”变量的代码。 Monsur Hossain Tonmoy,所以只是为了完成它(我没有足够的声望点来评论你的答案,对不起):
VisibleRegion vr = map.getProjection().getVisibleRegion();
double bottom = vr.latLngBounds.southwest.latitude;
Location center = new Location("center");
center.setLatitude(vr.latLngBounds.getCenter().latitude);
center.setLongitude(vr.latLngBounds.getCenter().longitude);
Location middleLeftCornerLocation = new Location("center");
middleLeftCornerLocation.setLatitude(center.getLatitude());
middleLeftCornerLocation.setLongitude(left);
float dis = center.distanceTo(middleLeftCornerLocation);
【讨论】:
先用
得到地图中心点googleMap.getCameraPosition().target;
然后计算出地图的宽度并获得 height/2 并使用您刚刚获得的 x y 值将它们转换为 Point。
然后将该点与地图相关联
LatLng widthPoint = map.getProjection().fromScreenLocation(point);
现在您有了 tagret 点和宽度点,您可以计算这两个点之间的距离,这将为您提供半径。
注意
这假设始终处于纵向模式,如果您处于横向模式,则圆圈将大于可见区域,因此在这种情况下,您需要获取与高度的距离
【讨论】: