In Windows 8, The positioner may not be available at any time, so we 'd better check the positioner status before using it.
The status can be obtained from the LocationStatus attribute in Geolocator.
PositionStatus indicates the PositionStatus of the enumerated type. There are 6 statuses: Ready, Initializing, NoData, Disabled, NotInitialized, and NotAvailable.
In addition, it is sometimes necessary to constantly check the status of the positioner. When the positioner is unavailable, a user-friendly prompt or other actions are provided. The Geolocator event StatusChanged is used to monitor changes in the locator status.
The code below shows that there is only one page in total.
The front-end XAML code is as follows:
Foreground XAML
<Page x: Class = "Win8Location. mainPage "xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "xmlns: local =" using: Win8Location "xmlns: d =" http://schemas.microsoft.com/expression/blend/2008 "xmlns: mc =" http://schemas.openxmlformats.org/markup-compatibility/2006 "mc: ignorable = "d"> <StackPanel Background = "{StaticResource ApplicationPageBackgroundThemeBrush}"> <Button x: name = "btnCheckStatusChanged" Content = "monitoring locator status" Click = "btnCheckStatusChanged_Click"/> <ScrollViewer> <TextBlock x: name = "txtMsg" TextWrapping = "Wrap" FontSize = "20"/> </ScrollViewer> </StackPanel> </Page>
The backend cs code is as follows:
Backend cs
Using System; using System. collections. generic; using System. IO; using System. linq; using Windows. devices. geolocation; using Windows. foundation; using Windows. foundation. collections; using Windows. UI. core; using Windows. UI. xaml; using Windows. UI. xaml. controls; using Windows. UI. xaml. controls. primitives; using Windows. UI. xaml. data; using Windows. UI. xaml. input; using Windows. UI. xaml. media; using Windows. UI. xaml. nav Igation; namespace Win8Location {public sealed partial class MainPage: Page {Geolocator geo = null; public MainPage () {this. initializeComponent ();} private void btnCheckStatusChanged_Click (object sender, RoutedEventArgs e) {btnCheckStatusChanged. isEnabled = false; if (geo = null) {geo = new Geolocator ();} txtMsg. text = DateTime. now. toString () + "> the positioner is started and the status is:" + geo. locationStatus + "\ n status description :" + GetDescription (geo. locationStatus); geo. statusChanged + = geo_StatusChanged;} async void geo_StatusChanged (Geolocator sender, StatusChangedEventArgs args) {PositionStatus statu = args. status; string msg = "\ n" + DateTime. now. toString () + "> the positioner status changes to:" + statu. toString (); msg + = "\ n status description:" + GetDescription (statu); await Dispatcher. runAsync (CoreDispatcherPriority. normal, () => {txtMsg. text + = m Sg ;}) ;}string GetDescription (PositionStatus statu) {string description = null; switch (statu) {case PositionStatus. Ready: description = "provides location data. "; Break; case PositionStatus. Initializing: description =" the location provider is Initializing. If GPS is a location data source and the GPS receiver in the view does not have the required number of attachments to obtain the exact location, this is the status. "; Break; case PositionStatus. NoData: description =" no available location data from any location provider. Before obtaining data from a location sensor, LocationStatus will have this value when the application calls the GetGeopositionAsync or the event handler that registers the PositionChanged event. After the data is available, the LocationStatus is converted to the Ready status. "; Break; case PositionStatus. Disabled: description =" the location provider is Disabled. This status indicates that the user has not been granted the application permission to access the location. "; Break; case PositionStatus. NotInitialized: description =" the search location operation has not been initialized. If the application has not yet called GetGeopositionAsync or registered an event handler for the PositionChanged event, LocationStatus may have this value. "; Break; case PositionStatus. NotAvailable: description =" Windows sensors and location platforms are not available in this version of Windows. "; Break; default: description =" Your positioner is too advanced, and the current technology cannot know its status :) "; break;} return description ;}}}
Run the following command: