程序setsmallestDisplacement在Androi

程序setsmallestDisplacement在Androi

本文介绍了融合的位置提供程序setsmallestDisplacement在Android中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi I have implemented Fused Location provider Api for getting location updates in a service. I was able to get the onlocationchanged event fired according to the interval set. However when i set the setsmallestDisplacement to 10 metres, the event is getting fired even if the device is still. Does any one have problem similar to this. please provide suggestions. Below is the code

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

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

To find the distance between two location values i have used this method.

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.是因为平板电脑正在室内使用吗?

But i get the distances as 43.51 , 49.32 , 520.02 even when the device is Still. Is it because the tablet am using in indoor?

推荐答案

如果为LocationRequest设置了位移",则当设备仍处于静止状态时,由于位移的优先级高于时间间隔"(时间间隔"和最快时间间隔"),因此无法获得位置.我猜想-可能您已将其他LocationRequest对象(未设置位移)传递给LocationServices.FusedLocationApi.requestLocationUpdates().

If the Displacement is set for LocationRequest, no chance of getting the locations if device is still as Displacement takes precedence over Intervals (interval and fastestInterval).As I could guess - May be you have passed different LocationRequest object (with no displacement set) to LocationServices.FusedLocationApi.requestLocationUpdates().

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

I was looking for an answer to how locs are received if both interval and displacement set. I just verified for myself with my application and below is the behavior for different configuration for LocationRequest:

  1. 未设置位移参数
    setInterval设置为1分钟,最快设置为1分钟.

  1. No Displacement parameter set
    setInterval is set to 1 min and fastest to 1 min.

我每1分钟收到一次位置.但是您会看到有时收到的信号很快,有时却收到的慢,这是因为setInterval不精确.

I received location every one mins. But you see sometimes it received fast and sometimes slower which is because setInterval is inexact.

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.

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分钟以上.

  1. Displacement parameter is set to 10 meters
    setInterval as above 1 mins.

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

No location updates are received if the device does not move or cross that distance. Between every onLocationChanged below I walked more than 10 meters so I received 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.

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.

这篇关于融合的位置提供程序setsmallestDisplacement在Android中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 21:00