I have taken this code from Stackoverflow and it was marked as working. I am just testing it but in my case it gives wrong result. the actual distance is about 139 km b/w these points but it show 2.07.. km. Method return distance in meter as mentioned then i convert it into km but it gives me wrong value. please help.
Asked
Active
Viewed 777 times
0

Yousaf Iqbal
- 71
- 6
-
2Post your code, not a screenshot. Do you expect us to type everything? – dambros Apr 15 '16 at 19:18
-
I will be careful next time. Got the solution. Thanks – Yousaf Iqbal Apr 15 '16 at 19:39
-
@YousafIqbal, please still [edit] your question and insert the code. Remember that you're not just asking for yourself, but for everyone else who finds this question/answer in search results. Code in an image never shows up in Google. Taking the time to post your code in the answer is part of being a responsible member of StackOverflow. Thank you! – CodeMouse92 Apr 15 '16 at 22:11
2 Answers
0
Check this and this link, you will definitely find needed result.
I used this formula, it worked:
double R = 6371.0; // km
double dLat = (lat2-lat1)*Math.PI/180.0;
double dLon = (lon2-lon1)*Math.PI/180.0;
lat1 = lat1*Math.PI/180.0;
lat2 = lat2*Math.PI/180.0;
double a = Math.sin(dLat/2.0) * Math.sin(dLat/2.0) +
Math.sin(dLon/2.0) * Math.sin(dLon/2.0) * Math.cos(lat1) * Math.cos(lat2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double d = R * c;
return d*1000;

Community
- 1
- 1

Goltsev Eugene
- 3,325
- 6
- 25
- 48
0
The first thing you'll need to do is figure out the coordinate system and projection system that you're using to calculate the distance. Could you provide the coordinates that you're using?
If you're using WGS84, the answer above will work. Otherwise, you'll need to re-project the points in the current coordinate system.

Nishanth Samala
- 1
- 2