How to respond to location updates (HTML)

Applies to Windows and Windows Phone

This topic explains how to respond to changes in the user's location.

What you need to know


  • Windows Runtime


You should be familiar with HTML and JavaScript.


Step 1: Verify that location is enabled

Make sure that the device has access to your location. You can find this setting on Windows by going to the Settings charm, selecting Change PC Settings, then Privacy, then Location. On Windows Phone, go to Settings, select location and set Location services to on.

Step 2: Open Microsoft Visual Studio

Create a new project, choosing a Blank App from the JavaScript/Store Apps project types.

Step 3: Create a New Project

In the New Project dialog box, choose a blank application from the JavaScript project types.

Step 4: Declare the Location Capability

Double click on package.appxmanifest in Solution Explorer for both the Windows and Windows Phone projects. Select the Capabilities tab. Check Location in the Capabilities list.

Step 5: Replace the JavaScript code

In the Shared project, open default.js (/js/default.js). Replace the code in the file with the following.

(function () {
    "use strict";

    var app = WinJS.Application;
    var activation = Windows.ApplicationModel.Activation;

    app.onactivated = function (args) {
        if (args.detail.kind === activation.ActivationKind.launch) {

                done(function () {

                    // Add an event handler to the button.

                    // Add an event handler to the button.


    var loc = null;

    function trackloc() {
        if (loc == null) {
            loc = new Windows.Devices.Geolocation.Geolocator();
        if (loc != null) {
            loc.addEventListener("positionchanged", onPositionChanged);
            loc.addEventListener("statuschanged", onStatusChanged);
            // display initial status, in case location is turned off.
            document.getElementById('geolocatorStatus').innerHTML =

    function stoptracking() {
        if (loc != null) {
            loc.removeEventListener("positionchanged", onPositionChanged);

    function onPositionChanged(args) {
        var pos = args.position;
        document.getElementById('latitude').innerHTML =
        document.getElementById('longitude').innerHTML =
        document.getElementById('accuracy').innerHTML =
        document.getElementById('geolocatorStatus').innerHTML =

    // Handle change in status to display an appropriate message.        
    function onStatusChanged(args) {
        var newStatus = args.status;
        document.getElementById('geolocatorStatus').innerHTML =

    function getStatusString(locStatus) {
        switch (locStatus) {
            case Windows.Devices.Geolocation.PositionStatus.ready:
                // Location data is available
                return "Location is available.";
            case Windows.Devices.Geolocation.PositionStatus.initializing:
                // This status indicates that a GPS is still acquiring a fix
                return "A GPS device is still initializing.";
            case Windows.Devices.Geolocation.PositionStatus.noData:
                // No location data is currently available
                return "Data from location services is currently unavailable.";
            case Windows.Devices.Geolocation.PositionStatus.disabled:
                // The app doesn't have permission to access location,
                // either because location has been turned off.
                return "Your location is currently turned off. " +
                    "Change your settings through the Settings charm " +
                    " to turn it back on.";
            case Windows.Devices.Geolocation.PositionStatus.notInitialized:
                // This status indicates that the app has not yet requested
                // location data by calling GetGeolocationAsync() or
                // registering an event handler for the positionChanged event.
                return "Location status is not initialized because " +
                    "the app has not requested location data.";
            case Windows.Devices.Geolocation.PositionStatus.notAvailable:
                // Location is not available on this version of Windows
                return "You do not have the required location services " +
                    "present on your system.";
                return "Unknown status.";


Step 6: Add the HTML for the apps

Open the default.html file for the Windows and Windows Phone projects, and copy the following HTML into inside the BODY tags of the file.

    <p>Geolocation Event Sample</p><br />
    <span id="status"></span><br />
    <button id="startTracking">Track Location</button><br />
    <br />
    <button id="stopTracking">Stop Tracking</button><br />
    Latitude: <span id="latitude">Waiting for update...</span><br />
    Longitude:  <span id="longitude">Waiting for update...</span><br />
    Accuracy (in meters): <span id="accuracy">Waiting for update...</span><br />
    Location Status: <span id="geolocatorStatus"></span><br />

Step 7: Build the app

Choose Build > Build Solution to build the project.

Step 8: Test the app

  1. On the Debug menu, click Start Debugging to test the solution.
  2. On Windows, the first time you run the sample, you'll get a prompt that asks if the app can use your location. Choose the Allow option.
  3. Click the Get Location button to get the current location.

Note  If location data doesn't display, check the following:

  • Make sure you've enabled access to location by opening package.appxmanifest in Solution Explorer and checking Location in the Capabilities tab.
  • On Windows, if an administrator has disabled location services, your app won't be able to access the user's location. In the desktop Control Panel, open Change Location Settings and check if Turn on the Windows Location platform is checked.
  • On Windows Phone, the user can disable location in the Settings app. Go to the Settings app, select location and make sure that the Location services switch is toggled on.


On Windows, you should test for location updates by using a Wi-Fi enabled computer, because the Windows Location Provider uses Wi-Fi triangulation to resolve the location. As the location is resolved, events that indicate updates are raised. If you use a computer that doesn't have Wi-Fi enabled, location will be based on IP address, and you might not get location update events.

If your application only needs to get geolocation data once, rather than subscribing to updates, use the GetGeopositionAsync method as described in Quickstart: Detecting location using the Windows Runtime Geolocation API.

Complete example

Related topics

Geolocation Sample



© 2014 Microsoft