启动依赖于设备位置的Android应用程序可能会非常困难。获取位置的经典方法是使用Android的核心API, 它已经存在很长一段时间,可能会混淆使用。为了简化这个过程,我们开发了Geolocator插件, 这使得开发人员能够从iOS,Android和Windows上的简单API的共享代码中获得位置 有时您可能想要或需要完全控制API, 然而,这就是新的谷歌Play服务位置API出现的地方。这些API简化了设备上多个传感器的位置感知,以帮助确定用户的位置。这意味着,当使用这些新的API,进行位置更新,查询更快更准确,最新更新(11.0.4),它从未容易上手。

入门

我们需要做的第一件事是确定我们的项目是达到了Android7.0并且安装了最新的Android支持库。

我们将要使用的API是Google释放出来的最新的NuGetGoogle 播放服务位置 11.0.4, 目前还是预发布。只要在NuGet检查预发布选项,搜索 Xamarin.GooglePlayServices.Location, 并在你的安卓应用中安装这个包:

除了NuGet,我们还需要确认我们在应用 的AndroidManifest中配置了位置权限:

位置采集

开发商曾创建谷歌API客户端,指定他们想要使用的API,连接到这个服务,并且使用他们想要的方式调用API。在v11中的所有位置APIs都发生了变化, 这些APIs都喝的查询当前FusedLocationProviderClient的能力并且能够标记调用。在调用时客户端需要传递两个参数:

  • LocationRequest: 位置更新的设置

  • LocationCallback: 当位置被更新时将接收回调的类。

创建一个位置回调

在MainActivity外,我们创建一个 LocationCallback的实现类,并创建一个可以在位置结果发布时可以被订阅的事件:

点击(此处)折叠或打开

  1. class MyLocationCallback : LocationCallback

  2. {

  3.     public EventHandler<Location> LocationUpdated;

  4.     public override void OnLocationResult(LocationResult result)

  5.     {

  6.         base.OnLocationResult(result);

  7.         LocationUpdated?.Invoke(this, result.LastLocation);

  8.     }

  9. }


我们还可以在MainActivity之外创建一个事件处理程序,我们可以我们想接收事件时订阅和取消订阅:

点击(此处)折叠或打开

  1. void OnLocationResult(object sender, Location location)

  2. {

  3.     //location.Latitude;

  4.     //location.Longitude;

  5. }



点击(此处)折叠或打开

  1. void OnLocationResult(object sender, Location location)

  2. {

  3.     //location.Latitude;

  4.     //location.Longitude;

  5. }



请求位置

在我们位置回调的地方,需要有时间来获得当前的FusedLocationClient并创建我们的请求:

点击(此处)折叠或打开

  1. MyLocationCallback locationCallback;

  2. FusedLocationProviderClient client;

  3.  

  4. async Task StartLocationUpdatesAsync()

  5. {

  6.     // Create a callback that will get the location updates

  7.     if(locationCallback == null)

  8.     {

  9.         locationCallback = new MyLocationCallback();

  10.         locationCallback.LocationUpdated += OnLocationResult;

  11.     }

  12.  

  13.     // Get the current client

  14.     if(client== null)

  15.         client = LocationServices.GetFusedLocationProviderClient(this);

  16.             

  17.     try

  18.     {

  19.     //Create request and set intervals:

  20.         //Interval: Desired interval for active location updates, it is inexact and you may not receive upates at all if no location servers are available

  21.         //Fastest: Interval is exact and app will never receive updates faster than this value

  22.         var locationRequest = new LocationRequest()

  23.                                   .SetInterval(10000)

  24.                                   .SetFastestInterval(5000)

  25.                                   .SetPriority(LocationRequest.PriorityHighAccuracy);

  26.  

  27.         await client.RequestLocationUpdatesAsync(locationRequest, locationCallback);            

  28.     }

  29.     catch(Exception ex)

  30.     {

  31.         //Handle exception here if failed to register

  32.     }

  33. }


是这么回事!有了这几行代码,当用户打开应用程序时,位置更新将继续流进来。

了解更多

在使用位置服务时还需要考虑其他考虑因素, 例如处理用户关闭位置的情况,进入后台运行状态,并且,当然,当需要时却关闭位置更新服务。你可以找到一个全样本解决所有这些因素,包括允许的行为,在我的 GitHub上。