将位置放在用户周围一定半径的地图上 - android

发布于 2024-10-28 14:31:59 字数 827 浏览 3 评论 0原文

我正在开发一个 GPS 应用程序,并且正在考虑在用户周围放置标记的想法,如 僵尸奔跑SpecTrek 但我完全不知道如何找出用户周围的位置。

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

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

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

感谢您抽出时间,

Infinitifizz

I am working on a GPS application and was toying with the idea of putting markers around the user like in Zombie Run and SpecTrek but am completely confused about how to find out the locations around the user.

I have been looking at the documentation for the Location class and have used the distanceTo() function for other things as well as MapView's latitudeSpan(), longitudeSpan() and getProjection() functions but I can't think how to decide on locations that are, for example, 100 metres around the user.

As I know the users position and I am only going to be placing markers that are ~1km away from the user, at the most, can I treat the area as flat rather than ellipsoidal and so then just could take the user's longitude and latitude and +/- from them to plot a marker around them (using some basic trigonometry such as x = cos(radius) and y = sin(radius) to keep it within the radius-sized circle around the player)?

I don't understand how long/lat correspond to actual scalar distances as in would 100long 100lat be 10 metres away from 90long 100lat? (I know these values are completely wrong but just using them to illustrate my question).

Thanks for your time,

Infinitifizz

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如果没有你 2024-11-04 14:31:59

两个经度/纬度点之间的距离通过半正矢公式计算。这是理论的链接:
http://www.movable-type.co.uk/scripts/latlong.html

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

如果您没有 PoIs 作为位置对象,只需像这样构建它们:

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

我在类似雷达的应用程序中使用了 distanceTo 方法,并且工作得很好。

The distance between two longitude/latitude points is calculated with the haversine formula. Here is a link with the theoretics:
http://www.movable-type.co.uk/scripts/latlong.html

I would use the distanceTo method which you already mentioned for your purpose. You have your current Location and all your Points of Interest. Just call Location.distanceTo(Poi) for each Point of Interest and if the distance is larger than 1000 meters you can draw the point to your map.

If you don't have your PoIs as Location objects just build them like this:

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

I used the distanceTo method in a radar like app and worked just fine.

嘦怹 2024-11-04 14:31:59

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

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));

A little closer to the bottom of the page the formular is described a little bit better. There you can see that he converted to radians before calculating. Also it is crucial that you use the right datatypes to avoid false rounding of numbers. Here is a small code snippet which should work:

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文