Android,位置不断变化,即使每个位置的准确性检查为20

时间:2018-04-20 06:36:38

标签: java android

我一直在开发一款能够跟踪用户跑步/走路的应用程序。

我将FusedLocationProviderClient与前台服务一起使用。我为LocationRequest对象设置了以下参数,

mLocationRequest.setInterval(3000);
mLocationRequest.setFastestInterval(500);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setSmallestDisplacement(6.4008) //7 yards

并且,为了获得位置更新,

mLocationCallback = new LocationCallback() {
       @Override
       public void onLocationResult(LocationResult locationResult) {
           super.onLocationResult(locationResult);
           Location location = locationResult.getLastLocation();

           if (mLocation == null) {      

               if (location.hasAccuracy() && isLocationAccurate(location)) {
                   mLocation = location;
                   isGpsAccurate = true;
               }

            } else {

                if (location.hasAccuracy() && isLocationAccurate(location))
                        calculateDistance(location);
            }
      }
};

mFusedLocationClient.requestLocationUpdates(mLocationRequest, 
                          mLocationCallback, Looper.myLooper());

以下是isLocationAccurate(location)的代码,

boolean isLocationAccurate(Location location) {

    float accuracy = location.getAccuracy();

    return accuracy > 0 && accuracy < MIN_ACCURACY;
}

以下是calculateDistance(location)的代码,

private calculateDistance(location){
     distance += mLocation.distanceTo(location);
     mLocation = location;
}

这是代码,在移动时效果很好,但在静止和室内(有时在室外)时,价值会不断变化。

我在这里做错了吗?

注意:我使用Strava和Runkeeper进行了验证。距离在那里保持不变。所以,我的设备(ONE PLUS 3T)工作正常。

1 个答案:

答案 0 :(得分:0)

*有一个getSpeed()函数

简单格式是

if(location.getSpeed() > 50){ calculateDistance(location); }

因此,如果设备处于移动状态,则意味着将开始跟踪,否则无需跟踪,只需在最后更新的位置显示标记