martskins
martskins

Reputation: 2940

Get device's moving speed

I'm working on a project on Sencha Touch and NodeJS.

What I am trying to do now is get the device's moving speed. From what I have seen in Sencha Docs I'm supposed to use Ext.device.Geolocation in order to use the device's geolocation services instead of the browser's.

Here's the way I'm doing it...

Ext.device.Geolocation.getCurrentPosition({
    success: function(position) {
        alert(position.coords.speed);
        alert(JSON.stringify(position))
    },
    failure: function() {
        console.log('something went wrong!');
    }
});

The position var in there gets this value when running from my iPhone..

{ busId: '186', position:'{"speed":null,"accuracy":81.15007979037921,
"altitudeAccuracy":10,"altitude":9.457816123962402,"longitude":-54.950113117326524,
"heading":null,"latitude":-34.93604986481545}'}

Which is missing the speed property, and I can't figure out why.

An even stranger thing happens though. If I access my app through an Android device, the position variable is empty. All I get is {}.

The app is running on localhost on my PC, and when I say access through my phone I mean I access to it with my PCs IP. I don't know if that is supposed to work ok or not though.

I have added the Cordova API, or tried to, in my projects folder, like this...

enter image description here

And included in my index.html like this...

<script type="text/javascript" src="./cordova-ios/CordovaLib/cordova.js"></script>

I have zero experience with Cordova so I have no idea what I'm doing here, what am I doing wrong?

Upvotes: 1

Views: 1904

Answers (1)

Jeff Wooden
Jeff Wooden

Reputation: 5479

According to the Sencha Touch 2 documentation, if the speed value returns null then the feature is unsupported on the device (http://docs.sencha.com/touch/2.3.1/#!/api/Ext.util.Geolocation). If it is supported then it should return at least 0.

If you don't plan on packaging the application for Android or iOS then there is no point in using Cordova. What I would suggest is writing your own getSpeed function which will check if the speed is not null or 0 and then use a fall back function.

For the fall back function, you'll need to be able to calculate the distance between two sets of GPS coordinates. There is a solution for that here: Calculate distance between 2 GPS coordinates

You will also need to track the time between the two GPS readings. The closer that you grab those readings, the more accurate your speed calculation will be. For instance, a 1 minute interval in between would be more accurate than a 10 minute interval since a vehicle or person could have stopped several times within that 10 minutes.

I would also make sure that you've set allowHighAccuracy to true on your Geolocation object. Also to prevent any cached GPS data, set maximum age to 0 in order to retrieve fresh data every time.

Upvotes: 2

Related Questions