在 Windows Phone 环境中模拟 GPS 位置信息

作者:源码世界时间:2014-03-18分类:WPF/SliverLight评论:0浏览:6243

1. [代码][WPF/SliverLight]代码     跳至 [1]
private readonly GeoCoordinateWatcher _gcw = new GeoCoordinateWatcher();
 
private readonly Random _random = new Random();
 
public GpsSamplePage()
{
    InitializeComponent();
 
    bool isSimulation = true;
    if (!isSimulation)
    {
        _gcw.PositionChanged += (s, e) => GcwPositionChanged(e);
        _gcw.Start();
    }
    else
    {
        Observable.Timer(TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(4))
        .Select(x =>
            new GeoPositionChangedEventArgs<GeoCoordinate>(
                new GeoPosition<GeoCoordinate>(DateTime.Now,
                    new GeoCoordinate()
                        {
                            // -90 <= latitude <= 90
                            Latitude = (_random.NextDouble() * 180.0) - 90.0,
                            // -180 <= longitude <= 180
                            Longitude = (_random.NextDouble() * 360.0) - 180.0
                        }
 
                ))).ObserveOnDispatcher().Subscribe(GcwPositionChanged);
    }
}
 
void GcwPositionChanged(GeoPositionChangedEventArgs<GeoCoordinate> e)
{
    Dispatcher.BeginInvoke(() =>
    {
        // thread safe
        tbLatitude.Text = e.Position.Location.Latitude.ToString();
        tbLongitude.Text = e.Position.Location.Longitude.ToString();
    });
}

如果您没有找到适合你的解决方案,请联系我们寻求帮助支付鼓励

标签:WPF/SliverLight  
返回顶部
分享按钮