Reputation: 213
Any good and gives accurate traveled distance example are welcomed. I tried by calculating the Latitude and Longitude. Storing previous and current points data and get the distance and adding for every 5 seconds when vehicle speed != 0 I checked with many rounds of moving vehicle, Every time I am getting different KMs distance. Not accurate. Any accuracy solution for moving vehicle path distance calculation.
private double journeyPathDistance(double currentLat, double currentLng) {
if (isFirstTime && vehicleSpeedValue != 0) {
Location loc1 = new Location("");
loc1.setLatitude(currentLat);
loc1.setLongitude(currentLng);
Location loc2 = new Location("");
loc2.setLatitude(currentLat);
loc2.setLongitude(currentLng);
isFirstTime = false;
prevLat = currentLat;
prevLng = currentLng;
distanceKms = loc1.distanceTo(loc2) / 1000;
} else {
if (vehicleSpeedValue != 0) {
Location loc1 = new Location("");
loc1.setLatitude(prevLat);
loc1.setLongitude(prevLng);
Location loc2 = new Location("");
loc2.setLatitude(currentLat);
loc2.setLongitude(currentLng);
prevLat = currentLat;
prevLng = currentLng;
distanceKms += (loc1.distanceTo(loc2) / 1000);
}
}
return distanceKms;
}
Upvotes: 0
Views: 795
Reputation: 1930
The question is not clear. However, if you're looking for the formula to calculate distance that can be found here: Calculate distance between Latitude/Longitude points. It includes Javascript code fragments that you can easily convert to Java.
Upvotes: 0
Reputation: 93678
You're going to have to add a lot of intelligence on top of that to get anything near accurate. Lets assume you're using GPS (if you're using network you're in huge trouble just from that). GPS on a phone is really only accurate to within 10 meters or so. So every data point you may be off by 10 meters. Add up a lot of 10 meter inaccuracies, and you're adding in a lot of extra distance. And you'll also get the occasional point that's just way off. There is no way to make that algorithm work by just adding all the deltas.
What you need to do is filter it. Ignore short distance changes, only add long ones where you have multiple confirmed readings (to ensure a random wrong location doesn't throw off your readings). The idea that immediately comes to mind is trying to detect turns by finding long lasting changes in heading, and figuring out where each straightaway begins and ends to find its length.
Of course the most accurate way to know that is just to read the odometer. But I presume you have a reason not to do things the easy way.
Upvotes: 0