Calculating the Speed from Android Device, using GPS Co-Ordinates

koeks525

Executive Member
Joined
Jul 14, 2012
Messages
5,315
Hi everyone,

I am trying to build an Android app which needs to get the user's speed as they are moving. Some quick googling reveals I will need to use the Android device's Accelerometer and/or get GPS coordinates from the device. I have managed to get GPS coordinates from the device, as the user starts moving (I tested this bit earlier in the week).

What would be the best way of getting the device's speed?

Thanks.
 

Ricard

Expert Member
Joined
Jul 6, 2007
Messages
2,819
LocationListener.getspeed() already give you it to in plain numbers... GPS's already give the speed and heading without anything special.
 

DominionZA

Executive Member
Joined
May 5, 2005
Messages
8,309
We have built an entire production system for the mines doing this.
The GPS data is fed to you in NMEA formatted sentences (assuming you getting same as us from the hardware). There are a variety of them, but you are interested in the GPRMC ones.
These sentences will give you speed, coords, etc...

http://www.gpsinformation.org/dale/nmea.htm#2.3

You can also take coords and date/time stamps from two of the sentences, work out distance travelled, and then speed.

EDIT: If you require more assistance, drop me a PM and I will respond with my email address. Can take it from there.
 

gkm

Expert Member
Joined
May 10, 2005
Messages
1,519
This is probably a good place to start reading:
http://developer.android.com/guide/topics/location/strategies.html

The location service does return the speed as Richard has mentioned.

But make sure to read the challenges section a couple of times. You are probably in for some pain if you want vaguely consistent results. If I could get a cent for every time I had to explain inconsistent location results, I would be a rich man today. But knowing some kind of location is still 100 times better than not knowing the location, so we still find what we get very valuable. But we know we have to take the information with a pinch of salt.
 

SBSP

Senior Member
Joined
Sep 7, 2007
Messages
663
Using PHP I use the below function to calculate distance.
if you take the previous long, lat and date/time compared the current long, lat and date/time you can work it out.

PHP:
function distanceGeoPoints ($lng1,$lat1,$lng2,$lat2) //Calculate long and lat
{
    $earthRadius = 3958.75;
	
	//$lng1='-25.848100';
	//$lat1='28.1757917';
	//$lng2='-25.848100';
	//$lat2='28.1757117';	
	
    $dLat = deg2rad($lat2-$lat1);
    $dLng = deg2rad($lng2-$lng1);


    $a = sin($dLat/2) * sin($dLat/2) +
       cos(deg2rad($lat1)) * cos(deg2rad($lat2)) *
       sin($dLng/2) * sin($dLng/2);
    $c = 2 * atan2(sqrt($a), sqrt(1-$a));
    $dist = $earthRadius * $c;

    // from miles
    $meterConversion = 1609;
    $geopointDistance = $dist * $meterConversion;

    return $geopointDistance;
}
 

gkm

Expert Member
Joined
May 10, 2005
Messages
1,519
SBSP, that formula is called the Haversine. Here it is in about every language you can dream off: https://rosettacode.org/wiki/Haversine_formula

Also, on Android you do not even have to code the formula. It is provided as a method call:
http://developer.android.com/refere...Between(double, double, double, double, float[]%29

The hassle using it for speed calculations is that unless you have pretty consistent location fixes, the speed calculation will jump around, even it the device is actually moving at constant speed. For example, if you first fix is 20 meters wrong in the one direction and the next fix is say 30 meters wrong in the other direction and you have actually moved 50 meters, then it will look as if you have moved 100 meters and your calculated speed is double your actual speed. And 30 meters is a pretty small error. Errors in the 500 meter range are pretty common, unless you run continuous locations which will drain the battery rapidly. You are probably better off using the speed provided by the device, which as far as I know is also derived from this and at least might have some smoothing.
 

SBSP

Senior Member
Joined
Sep 7, 2007
Messages
663
SBSP, that formula is called the Haversine. Here it is in about every language you can dream off: https://rosettacode.org/wiki/Haversine_formula

Also, on Android you do not even have to code the formula. It is provided as a method call:
http://developer.android.com/refere...Between(double, double, double, double, float[]%29

The hassle using it for speed calculations is that unless you have pretty consistent location fixes, the speed calculation will jump around, even it the device is actually moving at constant speed. For example, if you first fix is 20 meters wrong in the one direction and the next fix is say 30 meters wrong in the other direction and you have actually moved 50 meters, then it will look as if you have moved 100 meters and your calculated speed is double your actual speed. And 30 meters is a pretty small error. Errors in the 500 meter range are pretty common, unless you run continuous locations which will drain the battery rapidly. You are probably better off using the speed provided by the device, which as far as I know is also derived from this and at least might have some smoothing.

Never thought about it like that, Cant even remember where I scavenged the formula. didn't know it had a "name" Long ago I built this (One of many projects that I started and then got tired of it and never completed it.) :D
http://www.techtinker.co.za/geologic/index.php I used the formula create an email notification when my phone enters a set distance from a beacon placed on a google map. Works OK for that purpose

I only now realized that its converting to Miles :sick:
 
Top