根据时间间隔或位移获取位置更新 [英] Getting Location Updates based on time interval or Displacement

查看:68
本文介绍了根据时间间隔或位移获取位置更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Fused Location Api获取位置更新. 当我将x秒设置为时间间隔时,每隔x秒就会调用一次onLocationChanged(). 而且,当我将10米设置为minimumDisplacement时,直到用户从其原始位置移动10米后,才会调用onLocationChanged().

I am using Fused Location Api to get location updates. When I set x seconds as time interval then I got onLocationChanged() called after every x seconds. And when I set 10 meter as minimumDisplacement then onLocationChanged() is not called until user moves 10 meter from its original position.

但是当经过x秒或覆盖10米距离时,我需要调用onLocationChanged().

But I need to have onLocationChanged() called when either x seconds passed or 10 meter distance covered.

我知道如何实现这一目标.

any idea how I can achieve this.

我的代码

private Location mLastLocation;
public static final int REQUEST_LOCATION = 1006;
LocationRequest mLocationRequest;
private static final long POLLING_FREQ = 1000 * 10;
private static final long FASTEST_UPDATE_FREQ = 1000 * 10;
private static final long SMALLEST_DISPLACEMENT = 10;

mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setInterval(POLLING_FREQ);
        mLocationRequest.setFastestInterval(FASTEST_UPDATE_FREQ);
        mLocationRequest.setSmallestDisplacement(SMALLEST_DISPLACEMENT);

startLocationUpdates();

推荐答案

仅用于位移

  mLocationRequest.setInterval(0);
  mLocationRequest.setFastestInterval(0);
  mLocationRequest.setSmallestDisplacement(SMALLEST_DISPLACEMENT);

仅间隔

 mLocationRequest.setInterval(POLLING_FREQ);
 mLocationRequest.setFastestInterval(FASTEST_UPDATE_FREQ);
 mLocationRequest.setSmallestDisplacement(0); // Not needed, already default value is 0

通常使用AND计算间隔和距离参数.这意味着当您更改位置至少SMALLEST_DISPLACEMENT米,并且至少经过POLLING_FREQ毫秒后,就会触发onLocationChanged().

Normally interval and distance params are being calculated with AND. It means when you change your position at least SMALLEST_DISPLACEMENT meter AND at least POLLING_FREQ milliseconds have passed, then onLocationChanged() will be fired.

这篇关于根据时间间隔或位移获取位置更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆