0
I developed an application that one of the features it has is to calculate the distance traveled by the user, for this I use a Service and the API LocationListener to request user location updates.
In the method onLocationChanged I calculate the distance covered and saved in the local bank. What is happening is that sometimes this account of the distance covered is giving well above the estimated.
@Override
public void onLocationChanged(Location location) {
    if (location != null) {
        if (startCounter) {
            if (!isFirstLocationFinded) {
                latlon = new double[]{location.getLatitude(), location.getLongitude()};
                isFirstLocationFinded = true;
            } else {
                double distance = calculateDistance(location);
                    //save in database
                    TrackingRace track = database.find(TrackingRace.class, ID);
                    if (track == null) {
                        TrackingRace trackingRace = new TrackingRace(ID, distance);
                        database.save(trackingRace);
                    } else {
                        double oldDistance = track.distance;
                        double totalDistance = distance + oldDistance;
                        TrackingRace trackingRace = new TrackingRace(ID, totalDistance);
                        database.update(trackingRace);
                    }
                }
            }
        }
    }
}
private double calculateDistance(Location location) {
    double[] latlonNew = new double[]{location.getLatitude(),location.getLongitude()};
    double distance = 0;
    Location locationA = new Location("point A");
    locationA.setLatitude(latlon[0]);
    locationA.setLongitude(latlon[1]);
    Location locationB = new Location("point B");
    locationB.setLatitude(latlonNew[0]);
    locationB.setLongitude(latlonNew[1]);
    distance = locationA.distanceTo(locationB);
    latlon = latlonNew.clone();
    return distance;
}
The calculateDistance function will always calculate a straight line from point A to point B. That’s just what you want?
– Reginaldo Rigo
@Reginaldorigo I used this example https://github.com/vkuzub/Taximeter/blob/master/src/main/java/com/vkuzub/taximeter/service/CounterService.java
– Mateus Carvalho
I get it. You calculate that distance from time to time and it builds up. Is that? And you compare what even to conclude that it is above expected?
– Reginaldo Rigo
That I calculate every seven seconds or so, it’s like a taximeter. The comparison is that there is an estimate of distance traveled, the user informs the destination.. And I have the starting and finishing point of the percussion. In the end the calculated value is much higher than the estimate.. @Reginaldorigo
– Mateus Carvalho
You are calculating a straight line initially, which does not happen during the route. That’s why the accumulated distance is greater.
– Reginaldo Rigo
In the estimate is used the Maps API to calculate as if it were a route through the same streets... And the difference is quite big, for example: What was to give 3km is 18km.. @Reginaldorigo
– Mateus Carvalho
Hmmmm. So the error is in the routine that accumulates. She would have to do this: Initially she has point A. After 7 seconds I have point B. I calculate the distance from point B to point A. Accumulate and replace point A with point B.
– Reginaldo Rigo