LocationRequest 对象和 LocationChangeListener 之间有什么联系

What is the connection between LocationRequest object and LocationChangeListener

我一直在关注 this 我的应用程序需要获取当前位置更新,即使步行 10 步,或者可能每 2 分钟一次,但我认为基于时间的对电池的要求更高用法,在开始的文档中说:

your app specifies the required level of accuracy/power consumption and desired update interval, and the device automatically makes the appropriate changes to system settings. These settings are defined by the LocationRequest data object.

这是什么意思?精度级别和更新间隔?

之后在解释这些事情时他们说

To store parameters for requests to the fused location provider, create a LocationRequest. The parameters determine the level of accuracy for location requests.

这是否意味着我们可以使用 setInterval() 获取当前位置更新,例如更新 latlng?如果是这样,那为什么我们必须使用 onlocationchangelistener?如果位置请求已满足要求

谢谢

有两个关于位置的主要术语,它们是

  • 获取最后已知位置(旧位置)
  • 接收位置更新(新位置)

了解这些术语将有助于了解 LocationRequest 与其侦听器之间的关系。

获取最后的已知位置

位置 api 可以为给定提供者提供最后获得的位置,无需位置请求。该位置可以随时通过应用程序(包括您的应用程序)获取,例如 3 到 5 小时前。没有必要说 'hey provider please give me fresh location',所以我们不需要 LocationRequest 对象。

接收位置更新

这是我们需要说的地方'hey provider please give me fresh location at every 30 seconds with high accuracy'

要定义上述请求,我们需要创建 LocationRequest 对象。

为了接收我们请求的结果,我们需要创建监听器(接收过程是异步的)

这是LocationRequest和listener的关系


LocationRequest 对象有两个用途

  1. 如上所述,为融合提供者进行位置请求
  2. 提示对话框对位置请求提出足够的要求(例如准确度)