Dela via


Geofencing, start to finish (XAML)

[ This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation ]

Geofencing allows an app to define a geographical region and have the system alert the app when the device it's running on enters or exits that area.

With geofencing, your app can send a reminder to a user who is leaving work or arriving home. By setting up a series of geofences, you could display coupons whenever the user is near a coffee shop. Using cloud services like Azure Mobile Services, you could also create an app that notifies you when friends are nearby.

To use a geofence in your app, you'll first need to define a Geofence, then set up an event handler to listen for events, and finally, handle those events once they occur.

If you're new to developing Windows Store apps using C++, C#, or Visual Basic, take a look at Create your first Windows Store app using C# or Visual Basic to get up to speed.

Set up your geofences

You can start by reviewing the geolocation API's. The Detect the user's location topic will show how to get the user's location. The Respond to location updates topic will show how to get location updates.

Next you can review some best practices for working with geofences. These Guidelines for geofencing can help you make decisions about the design of your app and how you can incorporate geofencing into it.

One important decision will be to decide if your app will handle the geofence events in the foreground while your app is running or if you will set up a background task and then handle the event when the background trigger is fired.

These guidelines can also help you decide on the number and size of the geofences you want to create.

Set up a geofence

Once you have reviewed the best practices, you can set up one or more geofences. You'll also want to be sure the user has enabled the location permissions, in order to receive any geofence events.

 

Handle geofence notifications

Handle geofence notifications in the foreground

If you want to process geofence events in the foreground while your app is running, then you can add the event handlers to handle what happens when a geofence is entered, exited, or removed.

Listen for geofence events in the background

On the other hand, if you want to set up a background task to handle the geofence events, you must follow a number of steps to make sure your app is set up and configured by the user to work in the background.

Handle geofence notifications from a background task

If you've reviewed the Guidelines for geofencing and decided to set up a background task for your geofences, then you need to handle the events when they are triggered. This code will allow you to alert the user that a geofence event has occurred.

 

Test and debug your geofencing apps

Test and debug your geofencing apps

Once you have completed your geofencing app, you can use a variety of tools to test and debug it.

 

Want to know more?

Now that you have seen how geofencing works, here is some more info on related topics.

Windows Store app UI, start to finish (XAML)

Learn more about designing Windows Store app UI.

Roadmap for Windows Runtime apps using C# and Visual Basic and Roadmap for Windows Runtime apps using C++

Learn more about creating Windows Store apps using C++, C#, or Visual Basic in general.

Designing UX for apps

Learn more about designing great user experiences.

Display your location using Bing Maps

Learn how to detect the user's current location and display it on a map.

Guidelines for location-aware apps

Learn about some best practice for your location-aware app.