Windows 8 地理位置定位以及定位器状态监测
Windows  /  管理员 发布于 7年前   142
在Windows8中,定位器不一定随时可用,所以我们在使用定位器时最好先检查一下定位器的状态。
状态可以从Geolocator中的属性LocationStatus获得。
定位器状态是枚举类型PositionStatus,共有6种状态:Ready、Initializing、NoData、Disabled、NotInitialized、NotAvailable。
另外,有时还需要不断检测定位器的状态,当定位器不可用时给用户友好的提示,或做出其它的动作。Geolocator中有一个事件StatusChanged专门用来监测定位器状态的改变。
下面来看代码,总共只有一张页面。
前台XAML代码如下:
复制代码代码如下:
<Page
x:Class="Win8Location.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Win8Location"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Button x:Name="btnCheckStatusChanged" Content="监测定位器状态" Click="btnCheckStatusChanged_Click"/>
<ScrollViewer>
<TextBlock x:Name="txtMsg" TextWrapping="Wrap" FontSize="20"/>
</ScrollViewer>
</StackPanel>
</Page>
后台cs代码如下:
复制代码代码如下:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Devices.Geolocation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace Win8Location
{
public sealed partial class MainPage : Page
{
Geolocator geo = null;
public MainPage()
{
this.InitializeComponent();
}
private void btnCheckStatusChanged_Click(object sender, RoutedEventArgs e)
{
btnCheckStatusChanged.IsEnabled = false;
if (geo == null)
{
geo = new Geolocator();
}
txtMsg.Text = DateTime.Now.ToString() + ">定位器启动,状态为:" + geo.LocationStatus + "\n状态描述:" + GetDescription(geo.LocationStatus);
geo.StatusChanged += geo_StatusChanged;
}
async void geo_StatusChanged(Geolocator sender, StatusChangedEventArgs args)
{
PositionStatus statu = args.Status;
string msg = "\n\n" + DateTime.Now.ToString() + ">定位器状态改变为:" + statu.ToString();
msg += "\n状态描述:" + GetDescription(statu);
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
txtMsg.Text += msg;
});
}
string GetDescription(PositionStatus statu)
{
string description = null;
switch (statu)
{
case PositionStatus.Ready:
description = "提供位置数据。";
break;
case PositionStatus.Initializing:
description = "位置提供程序正在初始化。如果 GPS 是位置数据源,并且视图中的 GPS 接收器没有所需的附属数目来获取准确的位置,则此为该状态。";
break;
case PositionStatus.NoData:
description = "没有来自任何位置提供程序的可用位置数据。在可从位置传感器获取数据之前,LocationStatus 将在应用程序调用 GetGeopositionAsync或注册 PositionChanged 事件的事件处理程序时具有此值。数据可用后,LocationStatus 转换为 Ready 状态。";
break;
case PositionStatus.Disabled:
description = "位置提供程序已禁用。此状态指示尚未被授予该用户访问位置的应用程序权限。";
break;
case PositionStatus.NotInitialized:
description = "检索位置的操作尚未初始化。如果应用程序尚未调用 GetGeopositionAsync,或为 PositionChanged 事件注册事件处理程序,则LocationStatus 可能具有此值。";
break;
case PositionStatus.NotAvailable:
description = "Windows 传感器和位置平台在此版本的 Windows 中不可用。";
break;
default:
description = "您的定位器太先进了,目前的技术无法得知其状态:)";
break;
}
return description;
}
}
}
运行截图如下:
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号