Respond to location updates using HTML5 (HTML)

This topic shows you how to respond to changes in the user's geographic position, using the W3C Geolocation API in HTML5.


You should be familiar with HTML and JavaScript.


Step 1: Open Microsoft Visual Studio

Open an instance of Microsoft Visual Studio.

Step 2: 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 3: Create a New Project

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

Step 4: Enable the location capability

Double click on package.appxmanifest in Solution Explorer for both the Windows and Windows Phone projects, and select the Capabilities tab. Then check Location in the Capabilities list. This adds the Location device capability to the package manifest file.

    <!-- DeviceCapability elements must follow Capability elements (if present) -->
    <DeviceCapability Name="location"/>

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;
    var watchId;

    function watchloc() {
        if (loc == null) {
            loc = window.navigator.geolocation;
        if (loc != null) {
            watchId = loc.watchPosition(successCallback);

    function stopwatching() {

    function successCallback(pos) {
        document.getElementById('latitude').innerHTML = pos.coords.latitude;
        document.getElementById('longitude').innerHTML = pos.coords.longitude;
        document.getElementById('accuracy').innerHTML = pos.coords.accuracy;

    function errorCallback(error) {
        var strMessage = "";

        // Check for known errors
        switch (error.code) {
            case error.PERMISSION_DENIED:
                strMessage = "Access to your location is turned off. " +
                    "Change your settings to turn it back on.";
            case error.POSITION_UNAVAILABLE:
                strMessage = "Data from location services is " +
                    "currently unavailable.";
            case error.TIMEOUT:
                strMessage = "Location could not be determined " +
                    "within a specified timeout period.";
        document.getElementById("status").innerHTML = strMessage;


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.

    Geolocation Event Sample<br />
    <br />

    <button id="watchLoc">Watch Location</button><br />
    <button id="stopWatching">Stop Watching</button><br />
    Latitude: <span id="latitude">Waiting for update...</span><br />
    Longitude: <span id="longitude">Waiting for update...</span><br />
    Accuracy: <span id="accuracy">Waiting for update...</span><br />
    <span id="status"> </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. 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.


Location services uses a number of different sources to determine location. If GPS, cell towers, and Wi-Fi are unavailable, it will use the IP Address instead. In that case, note that you might not get any location update events, because IP Address data is not updated frequently.

Windows 10 geolocation sample

Windows 8.1 geolocation sample