24

嗨,我已经实现了 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);

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

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();
}

但即使设备静止,我也会得到 43.51 、 49.32 、 520.02 的距离。是因为平板电脑在室内使用吗?

4

2 回答 2

44

如果为 LocationRequest 设置了 Displacement,则如果设备仍为 Displacement 优先于 Intervals(interval 和fastestInterval),则没有机会获取位置。我可以猜到 - 可能是您已将不同的 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。

于 2015-06-03T10:42:47.520 回答
6

可能会发生您在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);
于 2015-01-05T11:45:33.267 回答