Windows 8 学习笔记(六)—Accelerormeter和GeoLocation

WinRT中的设备类库中包含多种设备信息,如Sensor重力加速计,Location位置信息,Bluetooth蓝牙,Sms信息收发,下面看看各种使用方式:

Sensor重力加速器

Accelerormeter重力加速计对象,通过该对象可以获取重力加速X\Y\Z的值

首先定义该对象

Accelerormeter _accelerometer = Accelerometer.GetDefault();

接下来通过ReadingChanged事件去跟踪X\Y\Z值,Shaken晃动事件去作一些逻辑处理

注册ReadingChanged事件

_accelerometer.ReadingChanged += _accelerometer_ReadingChanged;
void _accelerometer_ReadingChanged(Accelerometer sender, AccelerometerReadingChangedEventArgs args)
        {
            Dispatcher.InvokeAsync(CoreDispatcherPriority.Normal, (s, a) =>
            {
                AccelerometerReading reading = (a.Context as AccelerometerReadingChangedEventArgs).Reading;
                tbX.Text = String.Format("{0,5:0.00}", reading.AccelerationX);
                tbY.Text = String.Format("{0,5:0.00}", reading.AccelerationY);
                tbZ.Text = String.Format("{0,5:0.00}", reading.AccelerationZ);
            }, this, e);
        }

注册Shaken事件

_accelerometer.Shaken += _accelerometer_Shaken;
void _accelerometer_Shaken(Accelerometer sender, AccelerometerShakenEventArgs args)
        {
            Dispatcher.InvokeAsync(CoreDispatcherPriority.Normal, (s, a) =>
            {
                _shakeCount++;
                tbShakeCount.Text = _shakeCount.ToString();
            }, this, e);
        }

 

其中需注意两个属性:

MinimumReportInterval 用于获取重力感应器报告数据的最小间隔时间

ReportInterval 设置或获取报告间隔值,该值的设置可以反应加速计的敏感性

aa

 

GeoLocation位置信息

位置信息由Windows位置提供商通过WI-FI和IP 地址数据决定当前的地理信息,或者也可以通过GPS获取。通过GeoLocation可以得到准确的位置数据。

首先定义一个GeoLocation对象

Geolocator m_geo= new Geolocator();

接下来通过PositionChanged和 StatusChanged事件获取位置数据和当前位置设备器的状态

m_geo.PositionChanged += m_geo_PositionChanged;
Dispatcher.InvokeAsync(CoreDispatcherPriority.Normal, (s, a) =>
            {
//先得到当前位置对象
                Geoposition pos = (a.Context as IPositionChangedEventArgs).Position;
                textblock1.Text = "Latitude: " + pos.Coordinate.Latitude.ToString();
                textblock2.Text = "Longitude: " + pos.Coordinate.Longitude.ToString();
                textblock3.Text = "Accuracy: " + pos.Coordinate.Accuracy.ToString();
            }, this, e);

 

监视当前位置定位器的状态

m_geo.StatusChanged += m_geo_StatusChanged;
void m_geo_StatusChanged(Geolocator sender, StatusChangedEventArgs args)
        {
            Dispatcher.InvokeAsync(CoreDispatcherPriority.Normal, (s, a) =>
            {
                PositionStatus status = (a.Context as IStatusChangedEventArgs).Status;
                switch (status)
                {
                    case PositionStatus.Ready:
                        textblockStatus1.Text = "Ready";
                        break;
                    case PositionStatus.Initializing:
                        textblockStatus1.Text = "Initializing";
                        break;
                    case PositionStatus.NoData:
                        textblockStatus1.Text = "No data";
                        break;
                    case PositionStatus.Disabled:
                        textblockStatus1.Text = "Disabled";
                        break;
                    case PositionStatus.NotInitialized:
                        textblockStatus1.Text = "Not initialized";
                        break;
                    case PositionStatus.NotAvailable:
                        textblockStatus1.Text = "Not available";
                        break;
                    default:
                        textblockStatus1.Text = "Unexpected status";
                        break;
                }
            }, this, e);
        }

 

同样也有两个属性,这两个属性都是枚举类型:

PositionAccuracy:位置精准度,有0(默认)和1(高级别)两个值

PositionStatus:表明Geolocator提供位置数据的能力,上面例子也列出相应的枚举类型


 

 

Trackback:

http://www.cnblogs.com/jing870812/archive/2012/04/10/2441447.html

posted @ 2012-07-03 16:26  my favorite  阅读(243)  评论(0编辑  收藏  举报