windows phone7 学习笔记14——地理位置服务与反应性扩展框架

  使用Location Service能帮助开发者为windows Phone 开发具备位置感知(Location-Aware)功能的应用程序。比如很多导航的软件,查找附近吃饭、娱乐甚至厕所的应用程序,都是基于这个服务的。

  我们有3种方法来获取设备的位置。GPS,移动网络基站位置和WiFi位置。下面的图是这三种方式的优缺点:

  需要注意的是:windows phone会根据应用程序的需要选择一种或者多种方式来确定手机的位置。

  三种方式确定位置的优点是有效的平衡电池的消耗与位置信息的准确性。

  windows phone 为应用程序提供基于事件(event-driven)的统一接口。

  使用地理位置服务的建议:

  • 想办法减低电池的消耗;

      a. 如果可以的话 使用那个较低准确率的数据源;

      b.  当需要的时候打开地理位置服务,一旦使用完毕立刻关闭该服务。

  • 设置准确率的门限值,减低更新频率;
  • 使用状态更新事件(StatusChanged)监控服务状态,提醒用户状态的更新;
  • 提醒用户初次启动地理位置服务时需要等待一段时间(15秒到120秒)。
 

  使用位置服务

  1. 创建一个GeoCoordinateWatcher对象。
  2. 创建一个事件处理程序处理用户位置的改变。
  3. 在事件触发时抓取数据。
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Microsoft.Phone.Controls;
    using System.Device.Location;
    using Microsoft.Phone.Tasks;

    namespace Day13_LocationServices
    {
    publicpartialclass MainPage : PhoneApplicationPage
    {
    GeoCoordinateWatcher gcw;

    // Constructor
    public MainPage()
    {
    InitializeComponent();
    gcw.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(gcw_PositionChanged);
    gcw.Start();
    }

    void gcw_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
    {
    Latitude.Text = e.Position.Location.Latitude.ToString();
    Longitude.Text = e.Position.Location.Longitude.ToString();
    }
    }
    }

  反应性扩展框架(Reactive Extensions)

  • Reactive Extensions能够帮助应用程序把多种可监控的外部事件转换成异步消息;
  • 外部事件包括数据流(data streams),异步请求(asynchronous requests)和事件(event)等;
  • 使用Reactive Extensions,当外部时间触发的时候,应用程序得到异步的更新消息(asynchronous requests);
  • Reactive Extensions允许应用程序使用查询(query)操作来对时间进行过滤。
  
  如何使用Reactive Extensions可以参考msdn的这篇文章:http://msdn.microsoft.com/en-us/library/ff637517(VS.92).aspx
 
  
  参考资料:How to: Get Data from the Location Service for Windows Phone
       How to: Use Reactive Extensions to Emulate and Filter Location Data for Windows Phone

       Windows Phone 7 开发 31 日谈——第13日:位置服务

       http://www.cnblogs.com/porscheyin/archive/2010/12/23/1914300.html

原文地址:https://www.cnblogs.com/zhangkai2237/p/2360619.html