LatLng and distance between returning incorrect values on newer phones

佐手、 提交于 2019-12-12 02:06:42

问题


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
 }


来源:https://stackoverflow.com/questions/27256477/latlng-and-distance-between-returning-incorrect-values-on-newer-phones

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!