How to obtain device speed?

I use mirrorcell:geolocation-plus to obtain a device’s location, which works well.

I calculate the user’s distance traveled, using the device location. My calculations are correct, but with the inaccuracy of the reported lat/lon, even when the device is at rest, it is implied that the device is moving.

This error could be limited by taking the device speed into account. But, I don’t see a way of obtaining that information. mirrorcell’s geolocation object has a ‘speed’ property, but it’s always ‘Null’.

How to obtain the device speed?

Perhaps something like this?

// repeat every second

// 'noise suppression' 
if ( distance(currentLocation, newLocation) > errorMargin) { 
    currentLocation = newLocation;

locationMinuteAgo = pathOverTime[pathOverTime.length - 61];

speed = (distance(locationMinuteAgo, currentLocation) / 60)*60; // km/h

Thanks. That’s more or less what I’m doing now. The main issue with this is that it uses the same error-prone data, the location, to calculate the speed.

Specifically, I ignore small changes as well as large changes in the location reported by the device. This reduces the number of errors, but is very crude. After all, the user could be moving very slowly, or very fast.

HTML5 can provide the acceleration data of the device. So, I’m using that as an indication of whether an update in the position was achieved by actually moving the device. However, it turns out that even with a device at rest, it still reports (small and inconsistent) acceleration. And, this acceleration data is of course not always available.