Windows 8 地理位置定位 2.定位器状态监测

在Windows8中,定位器不一定随时可用,所以我们在使用定位器时最好先检查一下定位器的状态。
状态可以从Geolocator中的属性LocationStatus获得。

定位器状态是枚举类型PositionStatus,共有6种状态:Ready、Initializing、NoData、Disabled、NotInitialized、NotAvailable。

另外,有时还需要不断检测定位器的状态,当定位器不可用时给用户友好的提示,或做出其它的动作。Geolocator中有一个事件StatusChanged专门用来监测定位器状态的改变。

下面来看代码,总共只有一张页面。

前台XAML代码如下:

复制代码
前台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代码如下:

复制代码
后台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;
        }
    }
}
复制代码

运行截图如下:

 

 

posted @   成宇佳  阅读(1344)  评论(1编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示