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.

(string), and timestamp (Date) properties, if the geolocation provider supplies such data. 51<br />

You can indicate the accuracy you’re looking for through the Geolocator’s desiredAccuracy<br />

property, which is either PositionAccuracy.default or PositionAccuracy.high. The latter, mind you,<br />

will be much more radio or network intensive. This might incur higher costs on metered broadband<br />

connections and can shorten battery life, so set this to high only if it’s essential to your user experience.<br />

The Geolocator also provides a locationStatus property, which is a PositionStatus object<br />

containing ready, initializing, noData, disabled, notInitialized, and notAvailable. It should be<br />

obvious that you can’t get data from a Geolocator that’s in any state other than ready. To track this, you<br />

can listen to the Geolocator’s statusChanged event, where eventArgs.status property in your handler<br />

will contain a PositionStatus; this is helpful when you find that a GPS device might take a couple<br />

seconds to provide a reading. For an example of using this event, see Scenario 1 of the Geolocation<br />

sample in the Windows SDK:<br />

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

geolocator.addEventListener("statuschanged", onStatusChanged); //Remember to remove later<br />

function onStatusChanged(e) {<br />

switch (e.status) {<br />

// ...<br />

}<br />

}<br />

Note that PositionStatus and statusChanged reflect the readiness of the GPS device, and that<br />

readiness is not affected by the Location permission in the app’s Settings pane. As demonstrated in Here<br />

My Am!, an app needs to check permissions by trying to obtain a setting, which is a different concern<br />

from device readiness.<br />

The other two interesting properties of the Geolocator are movementThreshold, a distance in meters<br />

that the device can move before another reading is triggered (which can be used for geo-fencing<br />

scenarios), and reportInterval, which is the number of milliseconds between attempted readings. Be<br />

conservative with the latter, setting it to what you really need, because you again want to minimize<br />

network or radio activity. In any case, when the Geolocator takes and other reading and finds that the<br />

device has moved beyond the movement-Threshold, it will fire a positionChanged event, where the<br />

eventArgs.position property is a new Geoposition object. This is also shown in Scenario 1 of the<br />

Geolocation sample:<br />

geolocator.addEventListener("positionchanged", onPositionChanged);<br />

function onPositionChanged(e) {<br />

var coord = e.position.coordinate;<br />

document.getElementById("latitude").inner<strong>HTML</strong> = coord.latitude;<br />

document.getElementById("longitude").inner<strong>HTML</strong> = coord.longitude;<br />

51 That is, the civicAddress property might not be available or might be empty. An alternate means to obtain it is to use the<br />

Bing Maps API, specifically the MapAddress class, to convert coordinates into an address.<br />

399

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

Saved successfully!

Ooh no, something went wrong!