Lat longs give a point on a the earth which is approximated by a sphere. You can use spherical coordinates to convert the gps to xyz vectors from the center of the earth.
X=radius*cos(lat)*cos(lon)
Y=radius*cos(lat)*sin(lon)
Z=-radius*sin(lat)
If you then do a dot product between the two vectors you can find the angle between them. A dot B = len(A)*len(B)*cos(ang)
And finally you can use the formula for arcLength = radius*angle to get a distance.
That should come out to the following with two lat,Lon’s.
dist = acos(cos(lat1)*cos(lon1)*cos(lat2)*cos(lon2) + cos(lat1)*sin(lon1)*cos(lat2)*sin(lon2) + sin(lat1)*sin(lat2))*180/pi*radius
Where the radius of the earth is 6371 km.
You’d probably want to add the average of the altitudes of the two points too. 6371+(alt1+alt2)/2. But I’m unsure how much that would change things.
You probably could just measure the straight distance between two points instead. That would remove the dot product and arc length step.
Dist = radius*sqrt((cos(lat1)*cos(lon1)-cos(lat2)*cos(lon2))^2+(cos(lat1)*sin(lon1)-cos(lat2)*sin(lon2))^2+(sin(lat1)-sin(lat2))^2)
I factored out the radius but you could be more accurate using the altitude per point if you wanted.
You could try to find a js library to do the calculation too. No idea if they’d be doing anything beyond what’s being done here.
And just thinking aloud, but the gps coordinates have a varying accuracy so you might need to filter or smooth things out. For example if you’re standing still the gps coordinates may drift around by the accuracy amount so you’d get distance when you didn’t actually move. To filter you could only add distances longer than say a few feet. Or to smooth you could collect multiple lat Lon’s and average them before using it.