【问题标题】:How can I know the distance between two coordinates? [duplicate]我怎么知道两个坐标之间的距离? [复制]
【发布时间】:2020-06-23 19:01:54
【问题描述】:

也许这不是我要寻找答案的网站,但这是我的问题。

我正在编写一个 Java 程序,我需要知道两个位置之间的距离(以米为单位),因为它的坐标格式为 EPSG:4326。

例如,

坐标1:

42.34839, 2.484839

坐标2:

42.27345, 2.23453

从数学上来说,知道两个坐标之间的距离差的系统是什么?

【问题讨论】:

  • 离题 SO... "两点 P(x1,y1) 和 Q(x2,y2) 之间的距离由下式给出:d(P, Q) = √ (x2 − x1)^2 + (y2 − y1)^2"
  • Wikipedia 帮助.... @sleepToken 但不适用于球体(或几乎)上的坐标
  • 也许 Great-circle distance 在你的情况下就足够了

标签: java coordinates


【解决方案1】:

有很多不同的算法可以确定两点之间的距离。例如,曼哈顿距离很简单 IIRC,它只是 abs(x1 - x2) + abs(y1 - y2)。

class Coordinate {

    private final float x;

    private final float y;

    Coordinate(float x, float y) {
        this.x = x;
        this.y = y;
    }

    public float manhattanDistance(Coordinate other) {
        return Math.abs(x - other.x) + Math.abs(y - other.y);
    }

    public float getX() {
        return x;
    }

    public float getY() {
        return y;
    }
}
Coordinate first = new Coordinate(42.34839f, 2.484839f);

Coordinate second = new Coordinate(42.27345f, 2.23453f);

float manhattanDistance = first.manhattanDistance(second);

System.out.println("distance: " + manhattanDistance);

输出

distance: 0.32524872

【讨论】:

  • 曼哈顿距离不适用于球体(椭圆体)上的坐标。 X 中的单位必须与 Y 中的单位具有相同的长度,这对于经度和纬度来说是不正确的((至少如果你想要一个可用的结果))但是确实,有很多距离
猜你喜欢
  • 1970-01-01
  • 2010-11-23
  • 2012-08-06
  • 1970-01-01
  • 1970-01-01
  • 2020-02-12
  • 2018-06-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多