【发布时间】:2015-07-20 14:07:36
【问题描述】:
之前获取用户当前位置,我使用了 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);
}
它易于阅读且非常简单的代码。
但我注意到谷歌最近发布了New Client API Model in Google Play Services,并建议使用FusedLocationProviderApi,这看起来要复杂得多,它是异步的,它需要处理回调等。
与 LocationManager 相比,使用 FusedLocationProviderApi 有什么优势吗?
【问题讨论】:
-
它提供了更好的准确性和更少的电池消耗,因为它在 gps 和基于 WiFi 的位置之间切换。为了避免复杂性,我建议使用这个库:github.com/mcharmas/Android-ReactiveLocation
标签: android google-play-services android-location android-maps-utils