## Moving Speed out of GPS data

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

simon.schvartzman
Posts: 274
Joined: Tue Jul 29, 2014 12:52 am
Location: Brazil

### Moving Speed out of GPS data

Hi nerds (in the good sense), I've seen several old posts regarding the subject but none of them was conclusive and therefore I decided to open a new topic.

I would like to be able to calculate/show the speed I'm traveling on a car by using the GPS info in order to calculate it.

So far I've read the coordinates and the timestamp at fixed intervals then using the 'haversine' formula I can calculate the distance which divided by the timestamps' difference should give me the speed, right? Wrong...

Or may be partially right/wrong. The problem is that the result I get is extremely unstable. I'm driving at let say 30 km/hour the App shows the same result as the car's speedometer and suddenly it goes to 78 and back to 30.

By what I've read in the last several hours it is probably due to the GPS accuracy which takes me to the point of this post, has anybody a working script (willing to share) that would provide a reliable speed reading using the GPS coordinates (both on iPhone and Android)? Or at least a suggestion as how to implement it.

Many thanks
Simon
________________________________________
To ";" or not to ";" that is the question

jacque
VIP Livecode Opensource Backer
Posts: 4943
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

### Re: Moving Speed out of GPS data

I don't have a script, only an anecdote. During a recent hike, Google estimated my distance walked as 8.5 miles. I was going over some rugged terrain near the Mississippi River and had only actually walked the equivalent of a few city blocks. Looking at the tracking map later, Google showed that I had "walked" to the center of the river and back.

It looks like my phone lost GPS for a while and Google only picked up an approximate location at some point where there was a break in the foliage around me. Moral of the story, if Google can't get around it, I'm sure I couldn't either. We're at the mercy of the satellites.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

simon.schvartzman
Posts: 274
Joined: Tue Jul 29, 2014 12:52 am
Location: Brazil

### Re: Moving Speed out of GPS data

Hi @jacque, great story. I though Jesus was the only one able to walk on water but it seems according to Google you are as well. Of course Jesus didn't have a GPS to tell him he couldn't (nor did he have a LC forum to share...)

Now I wonder how Waze (or any other GPS running on the cell) deals with it. Comparing Waze speed readings with my car's speedometer I realised that Waze takes a lit bit more time to react to speed changes than the car meter.

I would guess that Waze is making some math (like average of the last readings, or dismissing points clearly out of the curve) before displaying the result. That's the algorithm I'd like to get instead of having to go to a lot of tries to find out it myself.

I have done some web research with no practical results...
Simon
________________________________________
To ";" or not to ";" that is the question

jacque
VIP Livecode Opensource Backer
Posts: 4943
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

### Re: Moving Speed out of GPS data

If you use Google Fit, you could work magic too. Seriously, anything more than simple arithmetic is beyond me, but we have some brilliant math gurus in our community. There are several on the mailing list who may lurk here as well, and @-hh is no slouch either. Maybe he'll have ideas.

Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

capellan
Posts: 607
Joined: Wed Aug 15, 2007 11:09 pm
Contact:

### Re: Moving Speed out of GPS data

Hi Simon,

Does your GPS code works on an Apple or an Android phone?
I am curious to learn how you are calculating it.

Al

simon.schvartzman
Posts: 274
Joined: Tue Jul 29, 2014 12:52 am
Location: Brazil

### Re: Moving Speed out of GPS data

HI @capellan, I'm running on iPhone but i guess it would be the same on Android.

The math is the following (assuming we are in the second cycle):

1 - at fixed intervals (2 seconds at the moment) I read the position and time stamp using

Code: Select all

`````` --get actual position and time
put mobileSensorReading ("location", true) into tDetails
put tDetails["latitude"] into tLatitude
put tDetails["longitude"] into tLongitude
put (tLatitude && "," && tLongitude) into ActPosition
put tDetails["timestamp"] into ActTimeStamp``````
2 - Get the ActualDistance to PreviousPosition using the 'haversine' formula

Code: Select all

``````function calcDistance ActPosition, PreviousPosition

if (point1 <> point2) then
put 6373 into pR  -- earth radius
-- get latitude and longitude of each point
put the item 1 of point1 into plat1
put the item 2 of point1 into plon1

put the item 1 of point2 into plat2
put the item 2 of point2 into plon2

put plat1 / 180 * pi into lat1
put plon1 / 180 * pi into lon1
put plat2 / 180 * pi into lat2
put plon2 / 180 * pi into lon2

put lat2 - lat1 into dlat
put lon2 - lon1 into dlon

put (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2 into a
put 2 * atan2( sqrt(a),sqrt(1-a)) into c

return pR * c
else
return 0
end if
end calcDistance``````
3 - Calculate ElapsedTime between readings (ActTime - PreviousTime)

4 - Get the speed in km/h as (ActualDistance * 60 * 60 )/ElapsedTime

5 - Save "Actuals" as Previous

Hope it helps, once I'm satisfied with the results I will share my code.
Any suggestions are welcomed.
Simon
________________________________________
To ";" or not to ";" that is the question