(地理)位置的通用数据结构/格式是什么?如何比较它们?

发布于 2024-09-08 11:22:34 字数 277 浏览 1 评论 0原文

通用(或独立于设备)物理位置的结构是什么?我的猜测是它可能是一个具有两个长字段的结构,或者类似的东西。

另外,给定一个目的地位置和两个候选位置,是否有一种简单的算法来确定哪个候选位置最接近目的地?我并不是真的在寻找一个可以处理所有这些的库或服务,尽管这可能是一个选项(在 Java 中),而是我想要一些非常简单的低级概念,我可以自己实际操作它们。

谢谢!

编辑 鉴于 f1sh 指出的计算的复杂性 - 是否有一个不错的小型 Java 库可以处理半正弦计算?

What is the structure of a generic (or device independent) physical location? My guess is it might be a struct with two long fields, or something similar.

Also, given one destination location, and two candidate locations, is there a simple algorithm for determining which candidate is closest to the destination? I'm not really looking for a library or service that handles all of this, though that could be an option (in Java), rather I want some very simple low level concepts that I can actually manipulate myself.

Thanks!

Edit
Given the intricasies of the calculations noted by f1sh - is there a nice small Java library that handles haversine calculations?

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

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

发布评论

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

评论(4

瞎闹 2024-09-15 11:22:34

正如您和 Andreas_D 已经提到的那样(2 个双字段),在类中存储纬度和经度对任何人来说都不应该成为问题。

棘手的部分是,计算该行星表面上 2 个点之间的距离并不像 2 个 2D 点之间的常见距离公式那么简单。
必须考虑以下事实:

  • 虽然纬度范围从 -90°(南极)到 +90°,但经度是周期性的。这意味着点 (0°, 179°) 到点 (0°, -180°) 的距离仅为 1°。
  • 地球是一个球体。这导致从纬度/经度到公制系统(我希望您正在使用......)的转换并不容易。赤道(即纬度 0°)的 1° 经度约为 111 公里,而在精确的北极,1° 经度为 0(公里/英寸/英尺/其他)。
  • 更多数学。

Storing latitude and longitude in a class should't be a problem to anybody, as you and Andreas_D already mentioned (2 double fields).

The tricky part is that calculating the distance between 2 Points on the surface of this planet is not as simple as the common distance formula between 2 2D-Points.
The following facts have to be considered:

  • While the latitude ranges from -90° (South Pole) to +90°, the longitude is periodical. That means that the point (0°, 179°) has a distance of only 1° to the point (0°, -180°).
  • Earth is a sphere. That results in the fact that conversion from lat/long to the metric system (which I hope you are using...) is not quite easy. 1° of longitude at the equator (which is at 0° latitude) is about 111km, whereas at the exact North pole 1° of longitude is 0 (km/inches/feet/whatever).
  • More math.
尽揽少女心 2024-09-15 11:22:34

GPS 设备通常提供以下有关位置的数据:

  1. 纬度 经度
  2. 高度
  3. 水平
  4. 精度
  5. 垂直精度

(更不用说速度、目的地、卫星等)

现在问题的第二部分:您可以使用半正矢公式来计算所有候选人之间的距离您的位置,然后按此距离对它们进行排序。不确定是否有更通用/科学的方法。

编辑:看看半正矢公式

GPS devices are commonly provides following data about location:

  1. Latitude
  2. Longitude
  3. Altitude
  4. Horizontal accuracy
  5. Vertical accuracy

(not to speak about speed, destination, satellites, etc.)

Now second part of your question: you can use haversine formula to calculate distance between all candidates and your location, and then sort them by this distance. Not sure about some more generic / scientific approach.

EDIT: Take a look at haversine formula here. Code example is also there. I don't think that you need some library for this.

树深时见影 2024-09-15 11:22:34

是的,像这样的类

public Geo {
  private double lat;
  private double lon;
}

足以存储地理位置。您可能需要添加 setter 方法来确保 lat、lon 始终在有效范围内,否则 Geo 对象可能具有无效状态。

Yes, a class like

public Geo {
  private double lat;
  private double lon;
}

is sufficient to store a geographical location. You might want to add setter method to make sure, that lat, lon are always in a valid range, otherwise a Geo object might have an invalid state.

北城半夏 2024-09-15 11:22:34

这显然是最准确的公式,半正弦在短直径上不准确:

//L = latitude, G = longtitude
double delta = G1 - G2;  
double p1 = cos(L2) * sin(delta);
double p2 = cos(L1) * sin(L2) - sin(L1) * cos(L2) * cos(delta);  
double p3 = sin(L1) * sin(L2) + cos(L1) * cos(L2) * cos(delta);
distance = 60 * Math.atan2(Math.sqrt(p1*p1 + p2*p2), p3);

参考

This is apparently the most accurate formula, the haversine being inaccurate over short diatances:

//L = latitude, G = longtitude
double delta = G1 - G2;  
double p1 = cos(L2) * sin(delta);
double p2 = cos(L1) * sin(L2) - sin(L1) * cos(L2) * cos(delta);  
double p3 = sin(L1) * sin(L2) + cos(L1) * cos(L2) * cos(delta);
distance = 60 * Math.atan2(Math.sqrt(p1*p1 + p2*p2), p3);

Reference

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