融合位置提供程序setsmallestDisplacement在Android中不起作用

hem*_*mar 19 android location fusedlocationproviderapi

嗨我已经实现了Fused Location提供程序Api来获取服务中的位置更新.我能够根据间隔设置获取onlocationchanged事件.但是,当我将setsmallestDisplacement设置为10米时,即使设备静止,事件也会被触发.有没有人有类似的问题.请提供建议.下面是代码

    mlocationrequest = LocationRequest.create();
    mlocationrequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mlocationrequest.setInterval(60000); // Update location every 1 minute
    mlocationrequest.setFastestInterval(10000);
    mlocationrequest.setSmallestDisplacement(10);


    LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mlocationrequest, this);
Run Code Online (Sandbox Code Playgroud)

为了找到两个位置值之间的距离,我使用了这种方法.

public static float distFrom (float lat1, float lng1, float lat2, float lng2 ) 
{
    double earthRadius = 3958.75;
    double dLat = Math.toRadians(lat2-lat1);
    double dLng = Math.toRadians(lng2-lng1);
    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
    Math.sin(dLng/2) * Math.sin(dLng/2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    double dist = earthRadius * c;

    int meterConversion = 1609;

    return new Float(dist * meterConversion).floatValue();
}
Run Code Online (Sandbox Code Playgroud)

但即使设备静止,我的距离仍为43.51,49.32,520.02.是因为平板电脑在室内使用吗?

cgr*_*cgr 39

如果为LocationRequest设置了Displacement,则如果设备仍处于Displacement优先于Intervals(interval和fasterInterval),则无法获取位置.我可以猜到 - 可能是你已经将不同的LocationRequest对象(没有置换集)传递给LocationServices.FusedLocationApi.requestLocationUpdates().

如果设置间隔和位移,我一直在寻找接收位置的答案.我刚刚使用我的应用程序验证了自己,下面是LocationRequest的不同配置的行为:

  1. No Displacement参数集
    setInterval设置为1分钟,最快设置为1分钟.

    我每隔一分钟就收到一个位置.但是你有时会看到它收到的速度很快而且有时会慢,这是因为setInterval是不精确的.

06-03 11:46:55.408 25776-25776/MyLocationListener:onLocationChanged.
06-03 11:47:56.008 25776-25776/MyLocationListener:onLocationChanged.
06-03 11:48:18.768 25776-25776/MyLocationListener:onLocationChanged.
06-03 11:49:22.938 25776-25776/MyLocationListener:onLocationChanged.
06-03 11:50:22.948 25776-25776/MyLocationListener:onLocationChanged.
06-03 11:52:22.978 25776-25776/MyLocationListener:onLocationChanged.
06-03 11:53:22.998 25776-25776/MyLocationListener:onLocationChanged.

  1. 位移参数设置为10米
    setInterval,大于1分钟.

    如果设备未移动或越过该距离,则不会收到位置更新.在每个onLocationChanged下面我走了10多米所以我收到了loc.

06-03 11:26:53.328 16709-16709/MyLocationListener:onLocationChanged.
06-03 11:35:38.318 16709-16709/MyLocationListener:onLocationChanged.
06-03 11:39:16.728 16709-16709/MyLocationListener:onLocationChanged.

  • 我的位移设置为10,setInterval和setFastestInterval设置为3分钟,即使有明显的移动我也没有收到定期的位置更新,不知道问题出在哪里? (2认同)

Meh*_*sar 5

可能会出现onLocationChanged描述位移大于10的方法不同的lat-long的情况,因此请核对您得到的lat-long并手动测量一次距离以交叉验证事物。

除了上述以外,还有一个可能的原因是优先事项,如果上述情况看起来不错,请尝试如下操作:

 mlocationrequest = LocationRequest.create();
mlocationrequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mlocationrequest.setSmallestDisplacement(10);
mlocationrequest.setInterval(60000); // Update location every 1 minute
mlocationrequest.setFastestInterval(10000);
Run Code Online (Sandbox Code Playgroud)