0

I am writing a distance tracker intended to track mileage for automobile drives with a mobile phone. I'm using cordova and cordova geolocation plugin. The code seems to run correctly where the geolocation plugin receives coordinates and I use the haversine formula to calculate the distance between two coordinates. However, my tracker is inaccurate because I am naively using lat/lon coordinates that are inaccurate. It appears that position has position.accuracy and position.speed. So I expect one or both of those properies may be useful.

accuracy: Accuracy level of the latitude and longitude coordinates in meters. (Number) speed: Current ground speed of the device, specified in meters per second. (Number)

My question is: are there known solutions for tracking distance that handle potentially inaccurate lat/lon information on a mobile phone?

More specific questions:

  • How accurate does position.speed tend to be on mobile phones?
  • Is there a position.accuracy that phones tend to gravitate towards? (I'm curious because I'm thinking of ignoring lat/lon cords with position.accuracy > threshold. So I'm trying to figure out what a good threshold would be)

Here's a snippet of my current code:

function DistanceTracker() {
    this._currentPosition = null;
    this.miles = 0.0;
}

DistanceTracker.prototype.start = function () {
    var self = this;
    navigator.geolocation.watchPosition(
        /*success*/function (position) {
            self.updateDistance(position);
        },
        /*fail*/function (errorPosition) { 
            //exception handling uses https://github.com/steaks/exceptions.js
            throw new exceptions.Exception("Error in geolocation watchPosition", { data: errorPosition }); 
        },
        { enableHighAccuracy: true, timeout: 5000 });
    )
};

DistanceTracker.prototype.updateDistance = function (position) {
    if (this._currentPosition !== null) {
        this.miles = this.miles + this.getDistanceFromLatLonInMiles(
            this._currentPosition.coords.latitude,
            this._currentPosition.coords.longitude,
            newPosition.coords.latitude,
            newPosition.coords.longitude);
    }
};

//Copied from http://stackoverflow.com/questions/27928/how-do-i-calculate-distance-between-two-latitude-longitude-points
function getDistanceFromLatLonInMiles(lat1,lon1,lat2,lon2) {
    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
    return d * KILOMETERS_TO_MILES_RATIO;
}

function deg2rad(deg) {
    return deg * (Math.PI/180);
}
Steven Wexler
  • 16,589
  • 8
  • 53
  • 80

1 Answers1

0

I would suggest using feet or meters instead of km for haversin. I found it to be much more accurate. I know it doesn't seem like it should make a difference. Just try it.

Replace var R = 6371; // Radius of the earth in km

With var R = 20890584;//feet

bentru
  • 3
  • 2