Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Learn how to detect the user's geographic ___location using APIs in the Windows.Devices.Geolocation namespace. This topic uses code examples, based on the geolocation sample, to demonstrate key points. For more info, download and run the sample.
Roadmap: How does this topic relate to others? See:
Step 1: Verify that ___location is enabled
Before your app can access ___location, Location must be enabled on the device. In the Settings app, check that the following ___location privacy settings are turned on:
- Location for this device... is turned on (not applicable in Windows 10 Mobile)
- The ___location services setting, Location, is turned on
- Under Choose apps that can use your ___location, your app is set to on
Step 2: Enable the ___location capability
Double click on package.appxmanifest in Solution Explorer and select the Capabilities tab. Then check Location in the Capabilities list. This adds the Location
device capability to the package manifest file.
<Capabilities>
<!-- DeviceCapability elements must follow Capability elements (if present) -->
<DeviceCapability Name="___location"/>
</Capabilities>
Step 3: Request access to the user's ___location
Request access to the user's ___location using the RequestAccessAsync method.
Important Starting in Windows 10, call the RequestAccessAsync before accessing the user’s ___location. At that time, your app must be in the foreground and RequestAccessAsync must be called from the UI thread. Until the user grants your app permission to their ___location, your app can't access ___location data.
using Windows.Devices.Geolocation;
...
var accessStatus = await Geolocator.RequestAccessAsync();
The RequestAccessAsync method prompts the user for permission to access their ___location. The user is only prompted once (per app). After the first time they grant or deny permission, this method no longer prompts for permission. To help the user change ___location permissions after they've been prompted, we recommend providing a link to the ___location settings as demonstrated later in this topic.
Step 4: Get the user's ___location and register for changes in ___location permissions
The GetGeopositionAsync method performs a one-time reading of the current ___location. Here, a switch statement is used with accessStatus (from the previous example) to act only when access to ___location is allowed. If allowed, the code creates a Geolocator object, registers for changes in ___location permissions, and requests the users ___location.
switch (accessStatus)
{
case GeolocationAccessStatus.Allowed:
_rootPage.NotifyUser("Waiting for update...", NotifyType.StatusMessage);
// If DesiredAccuracy or DesiredAccuracyInMeters are not set (or value is 0), DesiredAccuracy.Default is used.
Geolocator geolocator = new Geolocator { DesiredAccuracyInMeters = _desireAccuracyInMetersValue };
// Subscribe to StatusChanged event to get updates of ___location status changes
_geolocator.StatusChanged += OnStatusChanged;
// Carry out the operation
Geoposition pos = await geolocator.GetGeopositionAsync();
UpdateLocationData(pos);
_rootPage.NotifyUser("Location updated.", NotifyType.StatusMessage);
break;
case GeolocationAccessStatus.Denied:
_rootPage.NotifyUser("Access to ___location is denied.", NotifyType.ErrorMessage);
LocationDisabledMessage.Visibility = Visibility.Visible;
UpdateLocationData(null);
break;
case GeolocationAccessStatus.Unspecified:
_rootPage.NotifyUser("Unspecified error.", NotifyType.ErrorMessage);
UpdateLocationData(null);
break;
}
Step 5: Handle changes in ___location permissions
The Geolocator object triggers the StatusChanged event to indicate that the user's ___location settings changed. That event passes the corresponding status via the argument's Status property (of type PositionStatus). Note that this method is not called from the UI thread and the Dispatcher object invokes the UI changes.
using Windows.UI.Core;
...
async private void OnStatusChanged(Geolocator sender, StatusChangedEventArgs e)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
// Show the ___location setting message only if status is disabled.
LocationDisabledMessage.Visibility = Visibility.Collapsed;
switch (e.Status)
{
case PositionStatus.Ready:
// Location platform is providing valid data.
ScenarioOutput_Status.Text = "Ready";
_rootPage.NotifyUser("Location platform is ready.", NotifyType.StatusMessage);
break;
case PositionStatus.Initializing:
// Location platform is attempting to acquire a fix.
ScenarioOutput_Status.Text = "Initializing";
_rootPage.NotifyUser("Location platform is attempting to obtain a position.", NotifyType.StatusMessage);
break;
case PositionStatus.NoData:
// Location platform could not obtain ___location data.
ScenarioOutput_Status.Text = "No data";
_rootPage.NotifyUser("Not able to determine the ___location.", NotifyType.ErrorMessage);
break;
case PositionStatus.Disabled:
// The permission to access ___location data is denied by the user or other policies.
ScenarioOutput_Status.Text = "Disabled";
_rootPage.NotifyUser("Access to ___location is denied.", NotifyType.ErrorMessage);
// Show message to the user to go to ___location settings
LocationDisabledMessage.Visibility = Visibility.Visible;
// Clear cached ___location data if any
UpdateLocationData(null);
break;
case PositionStatus.NotInitialized:
// The ___location platform is not initialized. This indicates that the application
// has not made a request for ___location data.
ScenarioOutput_Status.Text = "Not initialized";
_rootPage.NotifyUser("No request for ___location is made yet.", NotifyType.StatusMessage);
break;
case PositionStatus.NotAvailable:
// The ___location platform is not available on this version of the OS.
ScenarioOutput_Status.Text = "Not available";
_rootPage.NotifyUser("Location is not available on this version of the OS.", NotifyType.ErrorMessage);
break;
default:
ScenarioOutput_Status.Text = "Unknown";
_rootPage.NotifyUser(string.Empty, NotifyType.StatusMessage);
break;
}
});
}
Step 6: Help the user change ___location settings
If the ___location settings don't allow your app to access the user's ___location, we recommend providing a convenient link to the ___location privacy settings in the Settings app. In this example, a Hyperlink control is used navigate to the ms-settings:privacy-___location
URI.
<!--Set Visibility to Visible when access to ___location is denied -->
<TextBlock x:Name="LocationDisabledMessage" FontStyle="Italic"
Visibility="Collapsed" Margin="0,15,0,0" TextWrapping="Wrap" >
<Run Text="This app is not able to access Location. Go to " />
<Hyperlink NavigateUri="ms-settings:privacy-___location">
<Run Text="Settings" />
</Hyperlink>
<Run Text=" to check the ___location privacy settings."/>
</TextBlock>
Alternatively, your app can call the LaunchUriAsync method to launch the Settings app from code. For more info, see How to launch the Settings app.
using Windows.System;
...
bool result = await Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-___location"));