将位置放在具有特定半径的用户周围 - android

时间:2011-04-02 17:28:45

标签: android gps android-mapview android-maps android-location

我正在开发一个GPS应用程序,并且想要在Zombie RunSpecTrek中围绕用户放置标记,但我对如何找出用户周围的位置感到困惑。

我一直在查看Location类的文档,并使用了distanceTo()函数来处理其他事情以及MapView的latitudeSpan(),longitudeSpan()和getProjection()函数,但我想不出如何决定例如,在用户周围100米的位置。

我知道用户的位置,我只会放置距离用户约1公里的标记,最多,我可以将该区域视为扁平而不是椭圆形,因此可以取得用户的经度和它们的纬度和+/-来绘制它们周围的标记(使用一些基本的三角函数,如x = cos(半径)和y = sin(半径),以使其保持在玩家周围的半径大小的圆圈内)?

我不明白多长/纬度对应于实际的标量距离,因为100长100lat距离90长100lat是10米? (我知道这些值是完全错误的,只是用它们来说明我的问题)。

感谢您的时间,

Infinitifizz

2 个答案:

答案 0 :(得分:2)

使用半正公式计算两个经度/纬度点之间的距离。这是与理论的联系: http://www.movable-type.co.uk/scripts/latlong.html

我会使用您已经提到过的distanceTo方法。您有当前的位置和所有兴趣点。只需为每个兴趣点调用Location.distanceTo(Poi),如果距离大于1000米,您可以将该点绘制到地图上。

如果您没有PoI作为Location对象,只需按照以下方式构建它们:

poiLocation = new Location(LocationManager.PASSIVE_PROVIDER);
poiLocation.setLatitude(latitude);
poiLocation.setLongitude(longitude);

我在类似app的雷达中使用了distanceTo方法并且运行得很好。

答案 1 :(得分:1)

稍微接近页面底部的公式更好一点。在那里你可以看到他在计算之前转换为弧度。此外,使用正确的数据类型以避免错误舍入数字至关重要。这是一个应该有效的小代码片段:

double lat1 = 52.104636;
double lon1 = 0.356324;

double R = 6371.0;
double d = 1.0;
double dist = d / R;
double brng = Math.toRadians(1.0);
lat1 = Math.toRadians(lat1);
lon1 = Math.toRadians(lon1);

double lat2 = Math.asin( Math.sin(lat1)*Math.cos(dist) + Math.cos(lat1)*Math.sin(dist)*Math.cos(brng));
double lon2 = lon1 + Math.atan2(Math.sin(brng)*Math.sin(dist)*Math.cos(lat1),            Math.cos(dist)-Math.sin(lat1)*Math.sin(lat2));
lon2 = (lon2+3*Math.PI)%(2*Math.PI) - Math.PI;

System.out.println("lat2: " + Math.toDegrees(lat2));
System.out.println("lon2: " + Math.toDegrees(lon2));