LatLng and distance between returning incorrect va

2019-09-18 15:07发布

问题:

My app works great on my old HTC One M7 in terms of calculating distance. However, on my wife's HTC One M8 and on my new Sony Xperia Z3, the distance it calculates is off. It's telling me that I've gone further than I really have. I've noticed this more when I'm hiking or running. If I'm skiing, it seems to be more accurate. I'm guessing that speed has something to do with it (if I'm going faster, the calculations are more accurate).

I'm calling a method to update my total distance on every location change which does this:

float[] distanceResults = new float[3];
double distanceResult = 0;
try
{
    Location.distanceBetween(location.getLatitude(), location.getLongitude(), previousLocation.getLatitude(), previousLocation.getLongitude(), distanceResults);
    distanceResult = distanceResults[0];
}
catch(IllegalArgumentException e)
{
    distanceResult = 0;
}
// add the new distance to the total distance
totalDistanceValue += distanceResult;

I can't wrap my head around why it doesn't calculate accurately on the newer phones. I thought about only calculating distance every 20th location change, but that seems like a ghetto fix for this...and may not even work.

anyone have any ideas why my total distance is more than it should be on newer phones when I'm going at a slower pace?

TIA

回答1:

When you need accuracy you have to use the accuracy of the location. Possibly you've seen the circle on google map get smaller as it zeros in on your location. That circle is based on the accuracy of the location.

@Override
public void onLocationChanged(Location location) {
        if (!location.hasAccuracy()) {
            return;
        }
        if (location.getAccuracy() > 4) {
            return;
        }

 //process location accurate to 4 meters
 }