Reputation: 1588
I am developing a mobile application which calculates distance based on the GPS. I am using the following haversine function to calculate distance.
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2-lat1); // deg2rad below
var dLon = deg2rad(lon2-lon1);
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c; // Distance in km
My concern is regarding how to handle the different location accuracy values.
ie, first I get a location with an accuracy of 30m and then I get a location with an accuracy of 150m.
To get a better distance calculation, which location do I need to consider/avoid for distance calculation? Or how can I adjust for these accuracy differences in my calculation using above formula?
I am hoping for advice from experts.
Upvotes: 0
Views: 3749
Reputation: 4085
Generally speaking it's not that easy.. As being mentioned in comment 'distance' in that respect can be a lot of different things depending on what your coordinates are. There are several Earth elliptical 'models' so you should consider this then writing your algorithm.
In case of GPS it's WGS84 projection. if you need fast and efficient solution take a look on Proj4 library which incorporate all this things inside itself and has bindings to almost everything.
Best way to go is to convert your coordinated into meters (but then you should take zone in consideration) and then make a calculation using rectangular coordinates. you might want to have a look in here. It proposes several ways of solving it.
inaccurate, but will do a job on non extreme locations and small distance. (never use it any any proper navigation software), but..
10001.965729km = 90 degrees
1km = 90/10001.965729 degrees = 0.0089982311916 degrees
10km = 0.089982311915998 degrees
Upvotes: 1
Reputation: 28727
Any of the know formulas like yours, or the built in on your smart phones API is acurate enough for small distances like GPS location to next location distance calculation.
But the main thing is not the distance formula.
You need to filter the GPS positions, you should not simply calculate the distance from one location to next.
Otherwise, especially at low speed movement you will sum up a zig zag line of inacurate GPS trajetory.
Upvotes: 2