Blu
Blu

Reputation: 4056

Calculate speed of moving person using PhoneGap

I am using PhoneGap API for making a Android/iPhone app. I'm using Geolocation API, and I want to calculate speed of moving object, but I don't understand how to do it.

Upvotes: 0

Views: 933

Answers (1)

fastr.de
fastr.de

Reputation: 1523

What you are looking for is the Haversine Formula.

Here is an javascript implementation from the leaflet sourcecode.

distanceTo: function (other) { // (LatLng) -> Number
    other = L.latLng(other);

    var R = 6378137, // earth radius in meters
        d2r = L.LatLng.DEG_TO_RAD,
        dLat = (other.lat - this.lat) * d2r,
        dLon = (other.lng - this.lng) * d2r,
        lat1 = this.lat * d2r,
        lat2 = other.lat * d2r,
        sin1 = Math.sin(dLat / 2),
        sin2 = Math.sin(dLon / 2);

    var a = sin1 * sin1 + sin2 * sin2 * Math.cos(lat1) * Math.cos(lat2);

    return R * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
}

latLng is an object with a lat and a lng attributes.

For speed, save the timestamps for both coordinates and calculate distance / time delta.

Upvotes: 1

Related Questions