diff options
Diffstat (limited to 'src/org/traccar/DistanceHandler.java')
-rw-r--r-- | src/org/traccar/DistanceHandler.java | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/src/org/traccar/DistanceHandler.java b/src/org/traccar/DistanceHandler.java index 9f174a6c7..7537e2867 100644 --- a/src/org/traccar/DistanceHandler.java +++ b/src/org/traccar/DistanceHandler.java @@ -34,23 +34,30 @@ public class DistanceHandler extends BaseDataHandler { public Position calculateDistance(Position position) { double distance = 0.0; + double totalDistance = 0.0; Position last = getLastPosition(position.getDeviceId()); if (last != null) { - if (last.getAttributes().containsKey(Position.KEY_DISTANCE)) { - distance = ((Number) last.getAttributes().get(Position.KEY_DISTANCE)).doubleValue(); + if (last.getAttributes().containsKey(Position.KEY_TOTAL_DISTANCE)) { + totalDistance = ((Number) last.getAttributes().get(Position.KEY_TOTAL_DISTANCE)).doubleValue(); } - if (position.getValid()) { - distance += DistanceCalculator.distance( + if (!position.getAttributes().containsKey(Position.KEY_DISTANCE)) { + distance = DistanceCalculator.distance( position.getLatitude(), position.getLongitude(), last.getLatitude(), last.getLongitude()); distance = BigDecimal.valueOf(distance).setScale(2, RoundingMode.HALF_EVEN).doubleValue(); + } else { + distance = ((Number) position.getAttributes().get(Position.KEY_DISTANCE)).doubleValue(); } } + if (!position.getAttributes().containsKey(Position.KEY_DISTANCE)) { + position.set(Position.KEY_DISTANCE, distance); + } + totalDistance = BigDecimal.valueOf(totalDistance + distance).setScale(2, RoundingMode.HALF_EVEN).doubleValue(); + position.set(Position.KEY_TOTAL_DISTANCE, totalDistance); - position.set(Position.KEY_DISTANCE, distance); return position; } |