Windows 8 學習筆記(六)—Accelerormeter和GeoLocation

來源:互聯網
上載者:User

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 設定或擷取報告間隔值,該值的設定可以反應加速計的敏感性

 

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提供位置資料的能力,上面例子也列出相應的枚舉類型

 

今天先整理這兩種裝置的用法及資訊的擷取,後續還要繼續學習其它裝置用法,待續。。。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.