zoukankan      html  css  js  c++  java
  • Windows 8 地理位置定位 3.位置变化跟踪

    如果在应用中不仅需要知道用户的位置,还需要根据位置的变化呈现不同的内容,则需要跟踪位置变化。这就用上定位核心类Geolocator中的PositionChanged事件了。
    Geolocator中有三个属性与该事件有关:

    1.public uint ReportInterval { get; set; }
    在位置更新之间请求的最小时间间隔(以毫秒为单位)。如果您的应用程序很少需要更新,则设置此值,以便位置提供程序可通过仅在需要时计算位置来节省电源。
    默认值为0,表示随时检测位置变化。

    2.public double MovementThreshold { get; set; }
    获取相对于来自最后的 PositionChanged 事件的坐标的移动距离(以米为单位),Geolocator 需要该移动距离来引发 PositionChanged事件。
    默认值为0.0,表示只要检测到位置发生变化就触发PositionChanged事件

    3.public PositionAccuracy DesiredAccuracy { get; set; }
    处于 Geolocator 的精度级别提供位置更新。PositionAccuracy 是枚举类型,共有两个值:Default和High。由于一般Windows 8 设备没有GPS,只有Wi-Fi和IP定位两种,很多情况下两种精度是一样的。

    下面是代码,仍然只有一个页面。

    前台XAML

    前台XAML
    <Page
        x:Class="Win8Location.PositionTracking"
        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">
    
        <Page.Resources>
            <x:Double x:Key="MyFontSize">20</x:Double>
            <Style TargetType="TextBlock">
                <Setter Property="FontSize" Value="{StaticResource MyFontSize}"/>
                <Setter Property="IsTextSelectionEnabled" Value="True"/>
            </Style>
            <Style TargetType="TextBox">
                <Setter Property="FontSize" Value="{StaticResource MyFontSize}"/>
                <Setter Property="Width" Value="100"/>
                <Setter Property="HorizontalAlignment" Value="Left"/>
            </Style>
        </Page.Resources>
    
        <StackPanel Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <ToggleSwitch x:Name="tsAccuracy" Header="定位精度(默认为低):" OffContent="低" OnContent="高" FontSize="{StaticResource MyFontSize}"/>
            <TextBlock Text="在位置更新之间请求的最小时间间隔(毫秒)(默认值为0,表示随时检测位置变化):"/>
            <TextBox x:Name="txtReportInterval"/>
            <TextBlock Text="触发PositionChanged事件的最小移动距离(米)(默认值为0.0,表示只要检测到位置发生变化就触发PositionChanged事件):"/>
            <TextBox x:Name="txtMovementThreshold"/>
            <Button x:Name="btnStartTracking" Content="开始定位并跟踪" Click="btnStartTracking_Click"/>
            <Button x:Name="btnStopTracking" Content="停止跟踪" Click="btnStopTracking_Click" IsEnabled="False"/>
            <ScrollViewer>
                <TextBlock x:Name="txtMsg" TextWrapping="Wrap"/>
            </ScrollViewer>
        </StackPanel>
    </Page>

    后台cs

    后台C#
     1 using System;
     2 using System.Collections.Generic;
     3 using System.IO;
     4 using System.Linq;
     5 using Windows.Devices.Geolocation;
     6 using Windows.Foundation;
     7 using Windows.Foundation.Collections;
     8 using Windows.UI.Core;
     9 using Windows.UI.Xaml;
    10 using Windows.UI.Xaml.Controls;
    11 using Windows.UI.Xaml.Controls.Primitives;
    12 using Windows.UI.Xaml.Data;
    13 using Windows.UI.Xaml.Input;
    14 using Windows.UI.Xaml.Media;
    15 using Windows.UI.Xaml.Navigation;
    16 
    17 namespace Win8Location
    18 {
    19     public sealed partial class PositionTracking : Page
    20     {
    21         Geolocator geo = null;
    22         public PositionTracking()
    23         {
    24             this.InitializeComponent();
    25             geo = new Geolocator();
    26         }
    27 
    28         async private void btnStartTracking_Click(object sender, RoutedEventArgs e)
    29         {
    30             btnStartTracking.IsEnabled = false;
    31             btnStopTracking.IsEnabled = true;
    32 
    33             geo.StatusChanged += geo_StatusChanged;
    34 
    35             if (tsAccuracy.IsOn)
    36             {
    37                 //提供可能获得的最准确的报告。这包括使用可能收费的服务或消耗更高级别的电池电量或连接带宽。High 的精度级别可能会降低系统性能,只有在必要时才使用。
    38                 geo.DesiredAccuracy = PositionAccuracy.High;
    39             }
    40             else
    41             {
    42                 //优化电源、性能和其他成本考虑。这是DesiredAccuracy的默认值.
    43                 geo.DesiredAccuracy = PositionAccuracy.Default;
    44             }
    45 
    46             uint reportInterval;
    47             if (uint.TryParse(txtReportInterval.Text, out reportInterval))
    48             {
    49                 geo.ReportInterval = reportInterval;
    50             }
    51 
    52             double movementThreshold;
    53             if (double.TryParse(txtMovementThreshold.Text, out movementThreshold))
    54             {
    55                 geo.MovementThreshold = movementThreshold;
    56             }
    57 
    58             Geoposition position = await geo.GetGeopositionAsync();
    59             string msg = DateTime.Now + ">定位开始\n";
    60             msg += "当前经度:" + position.Coordinate.Longitude + "\n";
    61             msg += "当前纬度:" + position.Coordinate.Latitude + "\n";
    62             msg += "定位精度(米):" + position.Coordinate.Accuracy + "\n\n";
    63             txtMsg.Text += msg;
    64             geo.PositionChanged += geo_PositionChanged;
    65         }
    66 
    67         async void geo_StatusChanged(Geolocator sender, StatusChangedEventArgs args)
    68         {
    69             string msg = DateTime.Now + ">状态:" + args.Status + "\n\n";
    70             await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    71             {
    72                 txtMsg.Text += msg;
    73             });
    74         }
    75 
    76         async void geo_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
    77         {
    78             string msg = DateTime.Now + "\n";
    79             msg += "当前经度:" + args.Position.Coordinate.Longitude + "\n";
    80             msg += "当前纬度:" + args.Position.Coordinate.Latitude + "\n";
    81             msg += "定位精度(米):" + args.Position.Coordinate.Accuracy + "\n\n";
    82             await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    83             {
    84                 txtMsg.Text += msg;
    85             });
    86         }
    87 
    88         private void btnStopTracking_Click(object sender, RoutedEventArgs e)
    89         {
    90             btnStartTracking.IsEnabled = true;
    91             btnStopTracking.IsEnabled = false;
    92             geo.StatusChanged -= geo_StatusChanged;
    93             geo.PositionChanged -= geo_PositionChanged;
    94         }
    95     }
    96 }

    为了方便测试位置移动的效果,这里用模拟器测试,截图如下:

  • 相关阅读:
    fastreport 中文汉字换行
    Delphi调用自身onchange事件,如提示缺少声明object时,不能调用,用此方法!
    delphi 截取某个字符之前的字段,,如 1234567-9,要分别得到
    delphi 四舍五入取整函数
    SQLServer2008不允许保存更改错误解决办法_不允许保存更改、不允许修改表解决_百度经验
    关于APP的架构实现!微博和知乎中的 feed 流是如何实现的?
    win2003没有OLE DB Provider for SQLServer驱动,可安装sqlserver2000或者安装MDAC2.6,适用于winxp、win2003
    SQLServer2000安装失败,[ODBC 驱动程序管理器]未发现数据源,详细信息请查看日志文件 sql2000 [Microsoft][ODBC 驱动程序管理器] 未发现数据源,参见sqlstp.org,直接退出
    sql server2008R2 密钥
    CentOS7安装memcached
  • 原文地址:https://www.cnblogs.com/chengyujia/p/2851766.html
Copyright © 2011-2022 走看看