使用Google播放服务简单快速更新Android位置
2017-09-17 21:59
295 查看
启动依赖于设备位置的Android应用程序可能会非常困难。获取位置的经典方法是使用Android的核心API,
它已经存在很长一段时间,可能会混淆使用。为了简化这个过程,我们开发了Geolocator插件,
这使得开发人员能够从iOS,Android和Windows上的简单API的共享代码中获得位置 有时您可能想要或需要完全控制API,
然而,这就是新的谷歌Play服务位置API出现的地方。这些API简化了设备上多个传感器的位置感知,以帮助确定用户的位置。这意味着,当使用这些新的API,进行位置更新,查询更快更准确,最新更新(11.0.4),它从未容易上手。
我们需要做的第一件事是确定我们的项目是达到了Android7.0并且安装了最新的Android支持库。
我们将要使用的API是Google释放出来的最新的NuGet的Google 播放服务位置 11.0.4, 目前还是预发布。只要在NuGet检查预发布选项,搜索 Xamarin.GooglePlayServices.Location, 并在你的安卓应用中安装这个包:
除了NuGet,我们还需要确认我们在应用 的AndroidManifest中配置了位置权限:
开发商曾创建谷歌API客户端,指定他们想要使用的API,连接到这个服务,并且使用他们想要的方式调用API。在v11中的所有位置APIs都发生了变化,
这些APIs都喝的查询当前FusedLocationProviderClient的能力并且能够标记调用。在调用时客户端需要传递两个参数:
LocationRequest: 位置更新的设置
LocationCallback: 当位置被更新时将接收回调的类。
在MainActivity外,我们创建一个 LocationCallback的实现类,并创建一个可以在位置结果发布时可以被订阅的事件:
点击(此处)折叠或打开
class MyLocationCallback : LocationCallback
{
public EventHandler<Location> LocationUpdated;
public override void OnLocationResult(LocationResult result)
{
base.OnLocationResult(result);
LocationUpdated?.Invoke(this, result.LastLocation);
}
}
我们还可以在MainActivity之外创建一个事件处理程序,我们可以我们想接收事件时订阅和取消订阅:
点击(此处)折叠或打开
void OnLocationResult(object sender, Location location)
{
//location.Latitude;
//location.Longitude;
}
点击(此处)折叠或打开
void OnLocationResult(object sender, Location location)
{
//location.Latitude;
//location.Longitude;
}
在我们位置回调的地方,需要有时间来获得当前的FusedLocationClient并创建我们的请求:
点击(此处)折叠或打开
MyLocationCallback locationCallback;
FusedLocationProviderClient client;
async Task StartLocationUpdatesAsync()
{
// Create a callback that will get the location updates
if(locationCallback == null)
{
locationCallback = new MyLocationCallback();
locationCallback.LocationUpdated += OnLocationResult;
}
// Get the current client
if(client== null)
client = LocationServices.GetFusedLocationProviderClient(this);
try
{
//Create request and set intervals:
//Interval: Desired interval
for active location updates, it is inexact and you may not receive
upates at all if no location servers are available
//Fastest: Interval is exact and app will never receive updates faster than this value
var locationRequest = new LocationRequest()
.SetInterval(10000)
.SetFastestInterval(5000)
.SetPriority(LocationRequest.PriorityHighAccuracy);
await client.RequestLocationUpdatesAsync(locationRequest, locationCallback);
}
catch(Exception ex)
{
//Handle exception here if failed to register
}
}
是这么回事!有了这几行代码,当用户打开应用程序时,位置更新将继续流进来。
在使用位置服务时还需要考虑其他考虑因素, 例如处理用户关闭位置的情况,进入后台运行状态,并且,当然,当需要时却关闭位置更新服务。你可以找到一个全样本解决所有这些因素,包括允许的行为,在我的 GitHub上。
它已经存在很长一段时间,可能会混淆使用。为了简化这个过程,我们开发了Geolocator插件,
这使得开发人员能够从iOS,Android和Windows上的简单API的共享代码中获得位置 有时您可能想要或需要完全控制API,
然而,这就是新的谷歌Play服务位置API出现的地方。这些API简化了设备上多个传感器的位置感知,以帮助确定用户的位置。这意味着,当使用这些新的API,进行位置更新,查询更快更准确,最新更新(11.0.4),它从未容易上手。
入门
我们需要做的第一件事是确定我们的项目是达到了Android7.0并且安装了最新的Android支持库。我们将要使用的API是Google释放出来的最新的NuGet的Google 播放服务位置 11.0.4, 目前还是预发布。只要在NuGet检查预发布选项,搜索 Xamarin.GooglePlayServices.Location, 并在你的安卓应用中安装这个包:
除了NuGet,我们还需要确认我们在应用 的AndroidManifest中配置了位置权限:
位置采集
开发商曾创建谷歌API客户端,指定他们想要使用的API,连接到这个服务,并且使用他们想要的方式调用API。在v11中的所有位置APIs都发生了变化,这些APIs都喝的查询当前FusedLocationProviderClient的能力并且能够标记调用。在调用时客户端需要传递两个参数:
LocationRequest: 位置更新的设置
LocationCallback: 当位置被更新时将接收回调的类。
创建一个位置回调
在MainActivity外,我们创建一个 LocationCallback的实现类,并创建一个可以在位置结果发布时可以被订阅的事件:点击(此处)折叠或打开
class MyLocationCallback : LocationCallback
{
public EventHandler<Location> LocationUpdated;
public override void OnLocationResult(LocationResult result)
{
base.OnLocationResult(result);
LocationUpdated?.Invoke(this, result.LastLocation);
}
}
我们还可以在MainActivity之外创建一个事件处理程序,我们可以我们想接收事件时订阅和取消订阅:
点击(此处)折叠或打开
void OnLocationResult(object sender, Location location)
{
//location.Latitude;
//location.Longitude;
}
点击(此处)折叠或打开
void OnLocationResult(object sender, Location location)
{
//location.Latitude;
//location.Longitude;
}
请求位置
在我们位置回调的地方,需要有时间来获得当前的FusedLocationClient并创建我们的请求:点击(此处)折叠或打开
MyLocationCallback locationCallback;
FusedLocationProviderClient client;
async Task StartLocationUpdatesAsync()
{
// Create a callback that will get the location updates
if(locationCallback == null)
{
locationCallback = new MyLocationCallback();
locationCallback.LocationUpdated += OnLocationResult;
}
// Get the current client
if(client== null)
client = LocationServices.GetFusedLocationProviderClient(this);
try
{
//Create request and set intervals:
//Interval: Desired interval
for active location updates, it is inexact and you may not receive
upates at all if no location servers are available
//Fastest: Interval is exact and app will never receive updates faster than this value
var locationRequest = new LocationRequest()
.SetInterval(10000)
.SetFastestInterval(5000)
.SetPriority(LocationRequest.PriorityHighAccuracy);
await client.RequestLocationUpdatesAsync(locationRequest, locationCallback);
}
catch(Exception ex)
{
//Handle exception here if failed to register
}
}
是这么回事!有了这几行代码,当用户打开应用程序时,位置更新将继续流进来。
了解更多
在使用位置服务时还需要考虑其他考虑因素, 例如处理用户关闭位置的情况,进入后台运行状态,并且,当然,当需要时却关闭位置更新服务。你可以找到一个全样本解决所有这些因素,包括允许的行为,在我的 GitHub上。
相关文章推荐
- Android服务使用方法 - 简单音乐播放实例
- Android 使用基于位置的服务(二)
- Android Service AIDL 远程调用服务之简单音乐播放实例
- [重要]使用Vitamio打造自己的Android万能播放器(10)—— 本地播放 (缩略图、视频信息、视频扫描服务)
- Android Service AIDL 远程调用服务之简单音乐播放实例【转载】
- Android歌词秀设计思路(6)运用Proxy设计模式简化歌词播放服务的使用
- android 使用MideaPlayer API来播放简单的应用
- 使用Google Cloud Messaging (GCM)服务实现Android消息推送
- 国行Android手机使用google全套GMS服务小结
- 使用Vitamio打造自己的Android万能播放器(10)—— 本地播放 (缩略图、视频信息、视频扫描服务)
- [重要]使用Vitamio打造自己的Android万能播放器(10)—— 本地播放 (缩略图、视频信息、视频扫描服务)
- Android中LocationManager的简单使用,获取当前位置[转
- Android简单、快速的网络库: Volley ( Google IO 2013 )
- 使用GCM服务(Google Cloud Messaging)实现Android消息推送
- Android中LocationManager的简单使用,获取当前位置(转http://www.javaeye.com/topic/363950)
- [原创]Android中LocationManager的简单使用,获取当前位置
- 使用GCM服务(Google Cloud Messaging)实现Android消息推送
- 【Android笔记 三】Location获取地理位置信息(中)Criteria类的简单使用
- Android Notification 使用详解之三:通过服务更新进度通知&在Activity中监听服务进度
- Android 使用基于位置的服务(一)