与 LocationManager 相比,使用 FusedLocationProviderApi 有什么优势吗? [英] Are there any advantages of using FusedLocationProviderApi over LocationManager?

查看:51
本文介绍了与 LocationManager 相比,使用 FusedLocationProviderApi 有什么优势吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前我使用 LocationManager 获取用户当前位置:

Earlier to get user current location I have used LocationManager:

LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);

if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

} else if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}

代码简单易读.

但我注意到谷歌最近发布了Google Play 服务中的新客户端 API 模型 并建议使用 FusedLocationProviderApi 看起来更复杂,它是异步的,需要处理回调等.

But I have noticed that Google recently released New Client API Model in Google Play Services and suggests to use FusedLocationProviderApi which looks like much more complicated, it is async, it requires to handle callbacks etc.

与 LocationManager 相比,使用 FusedLocationProviderApi 有什么优势吗?

Are there any advantages of using FusedLocationProviderApi over LocationManager?

推荐答案

FusedLocationProvider 使用混合硬件来根据请求的上下文确定位置,这意味着它已对您透明地优化.它还将缓存应用程序之间捕获的位置,以避免确定位置信息的不必要工作.因此,如果用户拥有各种位置感知应用程序,他们可能会避免对设备(和等待)进行位置捕获,因为这些应用程序可能已经被缓存.

FusedLocationProvider uses a mix of hardware to determine location based on the context of the request, meaning it's optimized transparently to you. It will also cache captured locations between applications to avoid unnecessary work to determine location info. So if a user has a variety of location-aware apps, they potentially avoid taxing the device (and waiting) for a location capture as one may have already been cached.

虽然 ol' LocationManager 在小型一次性情况下就足够了,但您绝对应该考虑使用较新的替代方案,因为好处可能很大,而且实施工作也很容易.

While the ol' LocationManager will suffice in small, one-off situations, you should definitely consider the newer alternative as the benefits may be great, and the work to implement, easy.

您也可以使用它,因为 Google Play 服务会定期跨设备更新,并且不断改进基于位置的功能(以及更多).

You may as well use it as Google Play Services is regularly updated across devices, and continuously includes improvements to location-based features (and more).

发布时 FusedLocationProvider 的解释链接:https://www.youtube.com/watch?v=Bte_GHuxUGc

A link to an explanation of the FusedLocationProvider at launch: https://www.youtube.com/watch?v=Bte_GHuxUGc

这篇关于与 LocationManager 相比,使用 FusedLocationProviderApi 有什么优势吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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