1

I use Google Maps API v3 in my java project. I need to find the Euclidean distance between two points. But when I use the ordinary formula for calculating the Euclidean distance the result is somehow wrong:

For example:

Let say I have two points New York (43.2994285, -74.2179326) and Stockholm (59.3293235, 18.0685808) and I want to calculate the Euclidean distance for these two points:

double distance = Math.sqrt(Math.pow(43.2994285 - 59.3293235, 2) + Math.pow(-74.2179326 - 18.0685808, 2)); distance = 93.66834091217483

the result is obvious wrong.

Then I realised that the beginning coordinate is not (0,0) as in Cartesian coordinate system. Actually the beggiinning is 51° 28' 38" N, 0° 00' 00" W (WGS 84). The referent point is Greenwich (51.4825766, -0.0076589).

So the procedure for calculating new coordinates is like this:

New York (|51.4825766 - 43.2994285|, |-0.0076589- (-74.2179326)|) = New York (8.1831481, 74.2102737)

Stockholm (|51.4825766 - 59.3293235|, |-0.0076589 - 18.0685808|) = Stockholm (7.8467469, 18.0762397)

And then apply the formula for Euclidean distance. My question is this a correct approach and is there any other way for doing this?

myanmar
  • 81
  • 1
  • 2
  • 11

2 Answers2

8

You need to use the Haversine formula to compute the great-circle distance between two points – that is, the shortest distance over the earth’s surface -- see here a description http://www.movable-type.co.uk/scripts/latlong.html

Alternatively (quote from here) if you need to use Euclidean distance

If the two points are near each other, for example in the same city, estimating the great circle with a straight line in the latitude-longitude space will produce minimal error, and be a lot faster to calculate. A minor complication is the fact that the length of a degree of longitude depends on the latitude: a degree of longitude spans 111km on the Equator, but half of that on 60° north. Adjusting for this is easy: multiply the longitude by the cosine of the latitude. Then you can just take the Euclidean distance between the two points, and multiply by the length of a degree:

distance(lat, lng, lat0, lng0):
    deglen := 110.25
    x := lat - lat0
    y := (lng - lng0)*cos(lat0)
    return deglen*sqrt(x*x + y*y)

and to speed that up further you can approximate the cos computation by using a polynomial approximation.

user2314737
  • 27,088
  • 20
  • 102
  • 114
  • 1
    Isn't it too much complicated. I know for this formula but is there any other simple way and I want to calculate it with Euclidean formula. – myanmar Mar 11 '15 at 18:27
  • 2
    no, this is the way. Eucledian distance would give you the distance when being able to cut through the earth, instead of the shortest distance over the earth's surface – Geert-Jan Mar 11 '15 at 18:29
  • Can I somehow change the two coordinates (New York, Stockholm) and using the Greenwich coordinate calculate new coordinates for New York and Stockholm on which we can use Euclidean formula. – myanmar Mar 11 '15 at 18:31
  • Can we observe the google map as a 2D plane and Greenwich coodinate as the beginning coordinate of our coordinate system and then all coordinates put in first quadrant from Greenwich point? – myanmar Mar 11 '15 at 18:52
0

Your metric would not work due to the concept of -curvature- in a 2dimensional surface.

Euclidean Metric is valid just for calculate very 'short' distances over Earth's surface, but for very large distances, like the one in your example, the curvature effect needs to be taken into account so you MUST use a metric that is accord to the geometry of a sphere surface: Haversine Formula.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140