23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Geolocation<br />

Before we explore sensors more generally, I’ll call out the geolocation capabilities for Windows Store<br />

apps separately because its API is structured differently from other sensors. We’ve already used this<br />

since Chapter 2, “Quickstart” in the Here My Am! app, but we need the more complete story of this<br />

highly useful capability.<br />

Unlike all other sensors, in fact, geolocation is the only one that has an associated capability you<br />

must declare in the manifest. Where you are on the earth is an absolute measure, if you will, and is<br />

therefore classified as a piece of personal information. So, users must give their consent before an app<br />

can obtain that information, and your app must also provide a Privacy Statement in the Windows Store.<br />

Other sensor data, in contrast, is relative—you cannot, for example, really know anything about a person<br />

from how a device is tilted, how it’s moving, or how much light is shining on it. Accordingly, you can use<br />

those others sensors without declaring any specific capabilities.<br />

As you might know, geolocation can be obtained in two different ways. The primary and most<br />

precise way, of course, is to get a reading from an actual GPS radio that is talking to geosynchronous<br />

satellites some hundreds of miles up in orbit. The other reasonably useful means, though not always<br />

accurate, is to attempt to find one’s position through the IP address of a wired network connection or to<br />

triangulate from the position of available WiFi hotspots. Whatever the case, WinRT will do its best to<br />

give you a decent reading.<br />

To access geolocation readings, you must first create an instance of the WinRT geolocator,<br />

Windows.Devices.Geolocation.Geolocator. With that in hand, you can then call its<br />

getGeopositionAsync method, whose results (delivered to your completed handler) is a Geoposition<br />

object (in the same Windows.Devices.Geolocation namespace, as everything here is unless noted).<br />

Here’s the code as it appears in Here My Am!:<br />

var gl = new Windows.Devices.Geolocation.Geolocator();<br />

gl.getGeopositionAsync().done(function (position) {<br />

//Save for share<br />

lastPosition = { latitude: position.coordinate.latitude,<br />

longitude: position.coordinate.longitude };<br />

}<br />

The getGeopositionAsync method also has a variation where you can specify two parameters: a<br />

maximum age for a cached reading (which is to say, how stale you can allow a reading to be) and a<br />

timeout value for how long you’re willing to wait for a response. Both values are in milliseconds.<br />

A Geoposition contains two properties. First, its coordinate property is a Geocoodinate object that<br />

provides accuracy (meters), altitude (meters), altitudeAccuracy (meters), heading (degrees relative to<br />

true north), latitude (degrees), longitude (degrees), speed (meters/sec), and a timestamp (Date). The<br />

second part of a Geoposition is a CivicAddress object named—what else!—civicAddress, which might<br />

contain city (string), country (string, a two-letter ISO-3166 country code), postalCode (string), state<br />

398

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!