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.
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);
var loc = new Windows.Devices.Geolocation.Geolocator();
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.
strStatus = "Location is available.";
strStatus = "Geolocation service is initializing.";
strStatus = "Location service data is not available.";
strStatus = "Location services are disabled. Use the " +
"Settings charm to enable them.";
strStatus = "Location status is not initialized because " +
"the app has not yet requested location data.";
strStatus = "Location services are not supported on your system.";
strStatus = "Unknown PositionStatus value (" +
newStatus.ToString() + ").";
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.
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.
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!
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.