Welcome, Guest. Please login or register.
Did you miss your activation email? News:

### Author Topic: What algorithm is used to calculate distance?  (Read 1872 times)

#### livfoss

• Jr. Member
•  • Posts: 52
• Keen not-so-young hiker and cyclist ##### What algorithm is used to calculate distance?
« on: March 18, 2015, 11:55:07 am »
I'm doing some analysis of .gpx files, and I realise that knowing two points (latitude, longitude) on the Earth's surface doesn't tell you the distance between them. For that you need a formula (algorithm). A popular one which is fairly accurate for short distances is Haversine. If you don't mind telling me, which algorithm does the Web Editor use? I can't make my distances the same as yours - they are out by quite a large margin, so I'm curious to know.

thanks for any info

#### livfoss

• Jr. Member
•  • Posts: 52
• Keen not-so-young hiker and cyclist ##### Re: What algorithm is used to calculate distance?
« Reply #1 on: March 19, 2015, 03:03:09 pm »
Actually, having re-worked my calculations, I see that I am not out all that far: about 5 percent, perhaps. That could be explained by different degrees of accuracy being used in the two calculations, as many individual distances between points are quite small and so are sensitive to variations in precision in the arithmetic.

#### pscheven

• Developer and support manager
• Administrator
• Sr. Member
•     • Posts: 462 ##### Re: What algorithm is used to calculate distance?
« Reply #2 on: March 20, 2015, 08:42:08 am »
Hello.

A simple well-known algorithm is used to calculate distance between two points in most of our applications :

Code: [Select]
`public static double DistanceTo(double lat, double lon, double lat2, double lon2) {            double distance = -1;            int earthRadius = 6371;//km            double dLat = (lat2 - lat) * (Math.PI / 180);            double dLon = (lon2 - lon) * (Math.PI / 180);            double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +                Math.Cos(lat * (Math.PI / 180)) * Math.Cos(lat2 * (Math.PI / 180)) *                Math.Sin(dLon / 2) * Math.Sin(dLon / 2);            double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));            distance = earthRadius * c;            return distance;}`
Best regards.
Patrick Schevenels.
Developer and support manager
Geolives S.A.

#### Ecanmadi

• Newbie
• • Posts: 1 ##### Re: What algorithm is used to calculate distance?
« Reply #3 on: January 24, 2019, 10:30:02 am »
What to do to be successful, there is an easier way.

#### Aliveindo

• Newbie
• • Posts: 2 ##### Re: What algorithm is used to calculate distance?
« Reply #4 on: April 23, 2019, 03:41:53 am »
This story can inspire me well. you for the good feeling That has come to share