Windows 8 app dev for the PhoneGap developer–Location

In my previous post about PhoneGap to Windows 8 migration, I used the location API as comparison point about how similar the programming approaches were. In this post I’m going to examine location in a bit more detail.

Location information in Windows 8, as in PhoneGap, can be accessed either in single requests, or can be subscribed to as the device moves around. The location information is the same in both platforms (latitude, longitude, altitude, etc) so that’s an easy match. Another interesting point about location data in a Windows 8 JavaScript app is the fact that you can either use the WinJS library to get the location, or if you want to stick strictly to HTML, you can use the W3C Geolocation API available in HTML5. For this post I’m going to show the WinJS technique.

To get continuous updates of the device’s location, rather than PhoneGap’s callback model of geolocation.watchPosition, Windows 8 uses an event handler model. Thus, the PhoneGap line

 var watchID = navigator.geolocation.watchPosition(onSuccess, onError);

becomes

 var loc = new Windows.Devices.Geolocation.Geolocator();
loc.addEventListener("positionchanged", onPositionChanged);

When you no longer want to receive location updates, you simply removeEventListener in the same way. It’s a good idea in general to turn off sensors when you don’t need them, to maximize battery power and application responsiveness. Another way to maximize battery life is to obtain the location information without needing the accuracy of GPS – in a lot of scenarios like weather and local events, you just need a general idea of the user’s location, which Windows 8 can determine by WiFi triangulation. In fact, a lot of devices out there that will be running Windows 8 (at least initially) will likely not even have a GPS built in, so at least for the foreseeable future, you’re going to want to use the defaultvalue for the DesiredAccuracy property of the Geolocator, which is “low”.

In addition to the positionchanged event, there’s a statuschanged event that tells you about the sensors, as well as whether the application is permitted to access location data. Some sample code that might be used in a statuschanged event handler is below. Note the case when the status is Disabled.

 switch (newStatus)
{
    case PositionStatus.Ready: 
        strStatus = "Location is available.";
        break;
    case PositionStatus.Initializing:
        strStatus = "Geolocation service is initializing.";
        break;
    case PositionStatus.NoData:
        strStatus = "Location service data is not available.";
        break;

    case PositionStatus.Disabled:
        strStatus = "Location services are disabled. Use the " +
                    "Settings charm to enable them.";
        break;

    case PositionStatus.NotInitialized:
        strStatus = "Location status is not initialized because " +
                    "the app has not yet requested location data.";
        break;

    case PositionStatus.NotAvailable:
        strStatus = "Location services are not supported on your system.";
        break;

    default:
        strStatus = "Unknown PositionStatus value (" + 
                    newStatus.ToString() + ").";
        break;
}

Speaking of permission to access location data, like many other phone platforms that PhoneGap supports, Windows 8 requires the developer to declare that they want to use the location capabilities of the device.  This in turn notifies the user when they install and/or run the app that it’s going to want to access their location.That’s easily specified in the application’s manifest file. Just open the file package.appxmanifest in solution explorer and check the Location box.

4.capabilities

The first time the user runs your app and it tries to access her location, she’ll actually get a prompt as to whether they want to allow that. The prompt interrupts the app so she can’t miss it.

locationprompt

Once the user decides to allow the app to access his or her location,, the permission stays that way until the user revokes it in the settings section of your app. You might be thinking to yourself “What settings section? I didn’t create a settings section!” Well one of the things that Windows 8 apps have in common is the Settings section accessible from the setting charm. That’s the panel users can bring up in Windows 8 on the right side of the screen. Just swipe in from the right side or press the Windows key and the C key at the same time. Because your app specified the Location capabilities, your app will automatically get as settings section for Permissions. You can also add your own sections and content to the settings panel, which makes it really flexible!

LocationSettings

Another way you can reset the permissions is to uninstall the app and reinstall it. In general this is great for development, because every time you uninstall and reinstall, you’re getting fresh settings and fresh saved data.

The last tip I’ll leave you with is this. When you are developing your app and running it on your local Windows 8 machine, it will use your machine’s location, typically triangulated from WiFi. But if you want to test your app as if you are in another location, you can do that by running it in the Windows 8 Simulator. Just chose Simulator at the top of the Visual Studio window. This actually brings up a remote desktop connection to the local machine. On the side of the simulator window there’s a globe icon, and if you click it, you can input coordinates, altitude and accuracy directly.

locationinsimulator

So, as you can say, it’s pretty straightforward to start using location services in Windows 8 JavaScript apps if you’ve done it before in PhoneGap.