1

I need to calculate speed after each 10 seconds or less (currently i am using fused location api to get the location after each 10 seconds). The problem is that the equipment is too slow and sometimes it gives the distance covers equal to zero.

I have tried using Location.distanceBetween() but it also produces zeros even when the equipment is moving. I have tried to calculate distance by a formula but sometimes distance is too small that it gives zero.

Now i want to calculate average speed. I want to save the points obtained in 1 minute (6 lat long values). And then after each 10 seconds, i want to calculate average speed between them. Thus after each 10 seconds I will add one points at the end and remove one point from the start. That will remove the possibility of zero.

Now is there any formula that can calculate speed or distance from set of lat long values or any better approach will be highly appreciated.

Hobbit
  • 601
  • 1
  • 9
  • 22

3 Answers3

3

You can calculate distance between two point, that are close enough, using simple geometry

deltaLngMeters = R * cos(latitude) * deltaLongitudeRadians;
deltaLatMeters = R * deltaLatitudeRadians;

whereas deltas are in radians, deltaLatitudeRadians = deltaLatitudeDegrees * pi / 180

Hence distance = sqrt(deltaLngMeters ^2 + deltaLatMeters ^ 2).

To sum up

function distance(point1, point2) {
    var degToRad = Math.PI / 180;
    return R * degToRad * Math.sqrt(Math.pow(Math.cos(point1.lat * degToRad ) * (point1.lng - point2.lng) , 2) + Math.pow(point1.lat - point2.lat, 2));
}

If you have array of six points, you can calculate average speed.

points = [{lat: .., lng: ..}, ... ]; // 6 points
distancesSum = 0; 
for(i = 0; i < distances.length - 1; i++) {
    distancesSum += distance(points[i], points[i + 1]);
}
return (distancesSum / (points.length - 1));

Yes, R is for the Earth radius, R = 6371000;// meters

shukshin.ivan
  • 11,075
  • 4
  • 53
  • 69
0

You can use multi threading(Thread.sleep()) to calculate a formula repeatedly for every 10 seconds. You can verify it here https://beginnersbook.com/2013/03/multithreading-in-java/.

Shree Naath
  • 477
  • 2
  • 5
  • 18
0

For small distances(hope the device won't move at speeds above 1 km/s), earth's surface can be treated as a plane. Then the latitude and longitude will be the coordinates of the device on the Cartesian plane attached to earth. Hence you can calculate the distance by this formula:

√(delta(longitude)^2 + delta(latitude)^2)

delta: difference

saga
  • 1,933
  • 2
  • 17
  • 44