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

标签 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);

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

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 的距离。是因为平板电脑在室内使用吗?

最佳答案

如果为 LocationRequest 设置了 Displacement,则如果设备仍然存在,则没有机会获取位置,因为 Displacement 优先于 Intervals(interval 和fastestInterval)。 我可以猜到 - 可能是您将不同的 LocationRequest 对象(没有设置位移)传递给 LocationServices.FusedLocationApi.requestLocationUpdates()。

如果同时设置了间隔和位移,我正在寻找如何接收 locs 的答案。我刚刚使用我的应用程序验证了自己,以下是 LocationRequest 的不同配置的行为:

  1. 无位移参数集
    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。

关于android - 融合的位置提供程序 setsmallestDisplacement 在 Android 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27778707/

相关文章:

android - 如何在 WebView 中显示 Android Action Bar

location - 在给定经纬度位置的情况下查找最近的街道

android - 减少 GPS 抖动

Android:使用 Fused Location Provider 检查是否启用了位置服务

java - JSON转换和应用程序兼容性问题

android - Google Play 在上传新版本时拒绝了应用

android - 如何将旧的小部件迁移到新的小部件

android - 即使应用程序被终止,flutter_background_geolocation 也会获取位置吗?

ios - 后台位置更新会导致应用程序强制终止吗?

java - 在后台更新位置并检查 LocationSettings