zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    }

    运行截图如下:

  • 相关阅读:
    dotnet 新项目格式与对应框架预定义的宏
    dotnet 线程静态字段
    dotnet 线程静态字段
    dotnet 通过 WMI 拿到显卡信息
    dotnet 通过 WMI 拿到显卡信息
    dotnet 通过 WMI 获取指定进程的输入命令行
    dotnet 通过 WMI 获取指定进程的输入命令行
    dotnet 通过 WMI 获取系统信息
    dotnet 通过 WMI 获取系统信息
    PHP show_source() 函数
  • 原文地址:https://www.cnblogs.com/chengyujia/p/2850914.html
Copyright © 2011-2022 走看看