2

I use algorithm (see: calculating distance between two points) to calculate distance between two points. but it get wrong result, see Java code:

/**
 * group1:set latitude to 89
 */
// set longitude1 = 0, longitude2 = 1
System.out.println(distance(89, 89, 0, 1, 0 , 0)); // 1941.0            result1

// set longitude1 = 0, longitude2 = 2
System.out.println(distance(89, 89, 0, 2, 0 , 0)); // 3881.0            result2

// set longitude1 = 0, longitude2 = 179
System.out.println(distance(89, 89, 0, 179, 0 , 0)); // 222381.0        result3

// set longitude1 = 0, longitude2 = 180
System.out.println(distance(89, 89, 0, 180, 0 , 0)); // 222390.0        result4

/**
 * group2:set latitude to 0
 */
// set longitude1 = 0, longitude2 = 1
System.out.println(distance(0, 0, 0, 1, 0 , 0)); // 111195.0            result5

// set longitude1 = 0, longitude2 = 2
System.out.println(distance(0, 0, 0, 2, 0 , 0)); // 222390.0            result6

// set longitude1 = 0, longitude2 = 179
System.out.println(distance(0, 0, 0, 179, 0 , 0)); // 19903892          result7

// set longitude1 = 0, longitude2 = 180
System.out.println(distance(0, 0, 0, 180, 0 , 0)); // 20015087          result8

In group1:

a = result2 - result1 = 1940 

b = result4 - result3 = 9

In group2:

c = result6 - result5 = 111195

d = result8 - result7 = 111195

It's very strange. c = d, group2 is right, but a ≠ b, group1 is wrong.

How do I solve this issue?

Is there other higher accuracy algorithm for calculate distance between two coordinate points?

1

No issue here - the formula calculates shortest path across the globe which in case #3 and #4 goes across the pole, not along a line of constant latitude which you are assuming.

Cross reference results using this tool here.

  • thank you very much, i get it. – Guo May 15 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.