0

Maybe this is not the website I'm looking for to find answers but here's my issue.

I'm working on a Java program and I need to know the distance in meters between two locations given it's coordinates in format EPSG: 4326.

For example,

coordinate 1:

42.34839, 2.484839

coordinate 2:

42.27345, 2.23453

What is the system, mathematically, to know the distance difference between two coordinates?

AlfaCoding
  • 91
  • 5
  • Off topic for SO... "*Distance between two points P(x1,y1) and Q(x2,y2) is given by: d(P, Q) = √ (x2 − x1)^2 + (y2 − y1)^2*" – sleepToken Mar 11 '20 at 18:02
  • [Wikipedia](https://en.wikipedia.org/wiki/Geodesics_on_an_ellipsoid) helps.... @sleepToken but not for coordinates on a sphere (or almost) – user85421 Mar 11 '20 at 18:15
  • maybe the [Great-circle distance](https://en.wikipedia.org/wiki/Great-circle_distance) would suffice in your case – user85421 Mar 11 '20 at 18:29

1 Answers1

0

There are a ton of different algorithms for determining the distance between two points. Manhattan distance for example is quite simple IIRC, it's just 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);

Output

distance: 0.32524872
Jason
  • 5,154
  • 2
  • 12
  • 22
  • Manhattan distance does not work for coordinates on a sphere (ellipsoid). Units in X must have the same length as in Y, which is not true for longitude and latitude ((at least if you want an usable result)) But true, there are a lot of distances – user85421 Mar 11 '20 at 18:22