Skip to content

Latest commit

 

History

History
 
 

Geolocation

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Geolocation sample

Shows how to use the Windows.Devices.Geolocation namespace to get the geographic location of the user's device and track when the location changes in various ways.

After obtaining access to the user's location, an app can

  • Retrieve the current location.
  • Receiving events when the location changes.
  • Receiving events when the device has entered or exited locations of interest.
  • Receiving events when the device has spent significant time in a location (a "visit").

Note: This sample is part of a large collection of UWP feature samples. If you are unfamiliar with Git and GitHub, you can download the entire collection as a ZIP file, but be sure to unzip everything to access shared dependencies. For more info on working with the ZIP file, the samples collection, and GitHub, see Get the UWP samples from GitHub. For more samples, see the Samples portal on the Windows Dev Center.

This sample also shows how to:

  • Use a geofence: Receive notifications when the user's device has entered or left an area of interest.
  • Handle changes in location permissions: Register for the Geolocator's StatusChanged event and use the StatusChangedEventArgs to determine the current position status.
  • Get location from a background task
  • Handle geofencing events from a background task
  • Handle geovisit events from a background task
  • Obtain satellite data: When available, use the GeocoordinateSatelliteData class to obtain additional information on the quality of the satellite based location data.
  • Display a toast: Notify the user when a geofencing event has occurred in the background.
  • Refresh geofence binding: Refresh the binding on resume and after removal of a geofence. Note that removal requires subscribing to removal events if a geofence is set as single use or a duration is set.
  • Request access to the user's location: Request access to the user's location using the RequestAccessAsync method. Important: 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.
  • Track when the user visits a location: Poll for or receive notifications when the user spends significant time in a location.
  • Help the user change location settings: Link to location privacy settings from your app if the user revokes access to location while your app is in the foreground. Call the LaunchUriAsync method with the URI "ms-settings://privacy/location".

Geofences need to be created in the Foreground Geofencing scenario (Scenario 4) and then you can go to Background Geofencing scenario (Scenario 5) to register for background geofencing events.

The Geolocation sample formats and parses time and dates in the en-US locale using the Gregorian calendar and 24-hour clock. To help other locales in entering data the edit fields have a format example shown below the control. For example, Start Time would be entered mm/dd/yyyy hh:mm:ss format. February 2, 2014 at 10:34 pm would be written 2/2/2014 22:34:00. For the Dwell Time and Duration the format is dd:hh:mm:ss so a time span of 7 days, 23 hours, 45 minutes and 55 seconds would be written as 7:23:45:55.

Declaring the Location capability

This sample requires that location capability be set in the Package.appxmanifest file to allow the app to access location at runtime. The capability can be set in the app manifest using Microsoft Visual Studio.

Related topics

Get current location
Windows.Devices.Geolocation
Windows.Devices.Geolocation.Geofencing
Guidelines for location-aware apps

Related samples

Traffic App sample
Lunch Scheduler app sample

System requirements

Client: Windows 10

Server: Windows Server 2016 Technical Preview

Phone: Windows 10

Build the sample

  1. If you download the samples ZIP, be sure to unzip the entire archive, not just the folder with the sample you want to build.
  2. Start Microsoft Visual Studio 2017 and select File > Open > Project/Solution.
  3. Starting in the folder where you unzipped the samples, go to the Samples subfolder, then the subfolder for this specific sample, then the subfolder for your preferred language (C++, C#, or JavaScript). Double-click the Visual Studio Solution (.sln) file.
  4. Press Ctrl+Shift+B, or select Build > Build Solution.

Run the sample

The next steps depend on whether you just want to deploy the sample or you want to both deploy and run it.

Deploying the sample

  • Select Build > Deploy Solution.

Deploying and running the sample

  • To debug the sample and then run it, press F5 or select Debug > Start Debugging. To run the sample without debugging, press Ctrl+F5 or selectDebug > Start Without Debugging.