0

I would like to know what's the best way to calculate the current speed with GPS. I've an external GPS receiver which is connected via USB to my car-notebook. It gives me just the following information: - Longitude - Latitude - Altitude

My try is to get two location-infos with timestamps. Then I am finding the difference in time (timestamp2 - timestamp1) and calculating the speed (distance/time).

Are there any other possibilites oder maybe any libraries available?

4
  • 2
    It's a bit strange that you're being given only the XYZ coordinates by the GPS. The standard GPS messages should include a LOT more information, so my guess is the device isn't sharing them with you (things like speed, number of satellites or other, more obscure data). Are there no settings for the GPS device you can tweak? Commented Nov 8, 2012 at 9:30
  • Mh, I think I could modify the firmware of the device to provide me some more infos. But I hoped the lang,lat and altitude are enough for my purposes. Commented Nov 8, 2012 at 9:39
  • It generally IS, but it feels like reinventing the wheel, given that the GPS should provide you with something as basic as speed. ;) Commented Nov 8, 2012 at 9:58
  • Thanks I will think about it. Commented Nov 8, 2012 at 11:09

1 Answer 1

4

To calculate the distance, you will need the Haversine Formula.

You will find many implementations of it around the web, here is one I use in C#:

private static double ArcInMeters(double lat0, double lon0, double lat1, double lon1)
{
    double earthRadius = 6372797.560856; // m
    return earthRadius * ArcInRadians(lat0, lon0, lat1, lon1);
}

private static double ArcInRadians(double lat0, double lon0, double lat1, double lon1)
{
    double latitudeArc = DegToRad(lat0 - lat1);
    double longitudeArc = DegToRad(lon0 - lon1);
    double latitudeH = Math.Sin(latitudeArc * 0.5);
    latitudeH *= latitudeH;
    double lontitudeH = Math.Sin(longitudeArc * 0.5);
    lontitudeH *= lontitudeH;
    double tmp = Math.Cos(DegToRad(lat0)) * Math.Cos(DegToRad(lat1));
    return 2.0 * Math.Asin(Math.Sqrt(latitudeH + tmp * lontitudeH));
}

private static double DegToRad(double x)
{
    return x * Math.PI / 180;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Isn't this calculation giving you distance 'as the crow flies'? What I mean, is if you're trying to calculate your movements inside a vehicle, you'd need to plot your co-ordinates against a map (Bing or Google, etc) and then determine the distance travelled from that, as that is your distance travelled in the vehicle.
@BrettRigby yes, this is the shortest distance between the two points. But I expect the points in the original question to be close enough in both time and space to expect the car is moving in a straight line between them. As the question is about how to calculate the current speed, and not the exact total way traveled, I think my approach should be OK.
@Timbo - Yes, please don't get me wrong, what you've provided is great; I commented simply to highlight the difference between the two for other readers looking to calculate driving speed. :o)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.