技术开发 频道

深入学习WP7之Reactive Extension

        模拟Location数据:

  Thread t = new Thread(StartLocationEmulation);

  t.Start();

  watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);

  watcher.MovementThreshold = 35;

  var gcwo = Observable.FromEvent>(

  eh =>watcher.PositionChanged +=eh,

  eh =>watcher.PositionChanged -=eh);

  watcher.Start();

  private void StartLocationEmulation()

  {

  var position = EmulatePositionChangedEvents().ToObservable();

  position.Subscribe(ev => wait_PositionChanged(null, ev));

  }

  static IEnumerable> EmulatePositionChangedEvents()

  {

  Random random = new Random();

  while (true)

  {

  double latitude = (random.NextDouble() * 180.0) – 90.0; // latitude is between -90 and 90

  double longitude = (random.NextDouble() * 360.0) – 180.0; // longitude is between -180 and 180

  yield return new GeoPositionChangedEventArgs(

  new GeoPosition(DateTimeOffset.Now, new GeoCoordinate(latitude, longitude)));

  Thread.Sleep(random.Next(2000));

  }

  }

  private void wait_PositionChanged(object sender, GeoPositionChangedEventArgs e)

  {

  Deployment.Current.Dispatcher.BeginInvoke(()=>MyPositionChanged(e));

  }

  private void MyPositionChanged(GeoPositionChangedEventArgs e)

  {

  this.textBlock1.Text = “Time: ” + e.Position.Timestamp. ToString(“yyyy-MM-dd hh:mm:ss”);

  this.textBlock2.Text = “Latitude: ” + e.Position.Location.Latitude.ToString(“0.00″);

  this.textBlock3.Text = “Longitude: ” + e.Position.Location.Longitude.ToString(“0.00″);

  }

0
相关文章