Maybe but NOT with sampling ‘every 5 seconds.’ and probably not
If you use the Navigate control, the distance you use (what you are trying with your present Blocks) will also fail since ORS servers will not respond within 5 seconds. Even if you could sample this frequently, OpenStreetMap may terminate your app for sampling too often I expect.
Speed? Actual or average? You probably cannot calculate instantaneous speed.
Continue to experiment. You may be able to calculate speed over a sampling interval (location changed event) and save that value then to allow you to determine the speed for the segments and later be able to put them together. In real time, I expect not
As @SteveJG says you can retrieve the “local speed” from the local location sensor and “remote speeds” ( if you need them ) as you retrieve the remote coordinates.
Should be easier an more accurate than calculating from distance/time
If not for the fact that 1 minute of long is equal to one minute of lat only at the Equator, you could be using the Pythagoras. However, as you get away from the Equator minutes of long remain same length (in say, meters or feet) while minutes of latitude get shorter. Hence you must correct the latitudinal difference using the cosine of the latitude.
I am trying to attach a blocks image to help you make your own. This is an actual blocks set from a program that uses gnss positioning data format. So it works. You just use this and adjust according to your case.
! The distance is, of course, expressed in same units (degrees or minutes as per your case) to convert to meters each 1 minute of distance is 1852 meters, give or take some inches. If you use degrees as units then each one degree has 60 minutes.
Yes. Haversines formula always for long distances and the result shows the distance along the ortho. The blocks I pasted are for local distances only - and the result is measured along the loxo.
TLDR the blocks I pasted should never be used for long distances. And beyond about 30 miles a difference will start being noticed between the results of the two formulas.
If you ran the .aia file, you'll notice that the difference will always be 2 seconds, and that's where it captures the coordinates.
The only detail would be:
In the first 2 seconds, you move from point A to point B.
And in the next 2 seconds, you move back from point B to A.
Logically, the coordinates should be the same, but that never happens. I'm speaking from experience testing it.
So there will always be a difference.
That's why I allocated 10 seconds to demonstrate that you're stopped, although I could also test the stop time at a longer time than 2 seconds, but that would be too close.
In OnGnmcReceived you also have speed and timestamp
So if using “locally” ( I mean using the gps of your local phone ) you already have a speed calculated by gps
If this is a a test ( I see in another thread you are showing the position of multiple cars on a map ) and in your final application you have to show position/speed/data of remote vehicles… you not only need the coordinates of the points but also the timestamp ( the exact moment in which these coordinates are recorded, as the one provided in OnGnmcReceived ), in remote communication you generally can’t rely on exact timings…
I would consider using another sensor to sense the VEHICLE STOPPED position.
Gnss positions are too erratic to judge this. Gnss is only good to determine speed between ticks that are far apart - e.g. 100 m and say 10 seconds, but they are not good to determine small movements say 1 m over 10 seconds.
Try combining sensors. E.g. gnss for longer distances and Accel / Gyro for checking if the car has / is stopped.
Dear @elmachi130,
i've fought a long time with the same problem: to determine the speed as delta(position)/delta(time). As distance calculation I was using the Haversine formula and as delta(time) I was using a clock whose tick was set to 250 milliseconds. This was necessary to have a fast response to the car movement (a historical rally car). In addition to the precision of the position obtained by the GPS, which is highly affected by the GPS sensor (on the hardware side) of the phone, you shall consider that you cannot rely on the timing accuracy as well. What I mean is that you cannot be sure that the tick of a clock is stable and therefore the delta(time) is "never" stable, and you shall compute it by reading the system time (the internal one of the phone) at every clock tick. This instability is, obviously, more sensitive when the clock tick is on milliseconds basis, rather than on seconds, but if the clock tick is based on seconds, and if you consider that at 40 km/h a car is running 11 meters in 1 second....you cannot have a tick based on seconds (i.e. 5 s). Another cause of error is the trajectory: if you compute the distance between two locations, fixed by the GPS, you shall also consider that the distance travelled is different if your travel has been streight or if you have taken a curve ! This is another reason to make the computations as faster as possible (within a few milliseconds, i.e. not slower than 250 ms) so to let the distance be considered streight even if taking a curve.
Honestly, at the end I gave up, and I've used the speed supplied directly by the GPS sensor (always read every 250 ms) which is quite accurate, as @davidefa has already suggested
Having said (all!) that, if you want I can try to search in my trash bin that old version of that code in which I've implemented that solution (the .aia) but I'm not sure that I kept it