Skip to content
Permalink
Browse files
CB-9355 Fix Geolocation plugin start watch fail related to unset Move…
…mentThreshold on Windows 10
  • Loading branch information
daserge committed Jul 14, 2015
1 parent 2397519 commit f88dfd3a7885ee0b596fccbd981d9f2ca4a19041
Showing 1 changed file with 72 additions and 52 deletions.
@@ -19,10 +19,30 @@ var PositionError = require('./PositionError'),
loc;

function ensureLocator() {
if (loc == null)
var deferral;

if (loc == null) {
loc = new Windows.Devices.Geolocation.Geolocator();

return loc;
if (typeof Windows.Devices.Geolocation.Geolocator.requestAccessAsync === 'function') {
deferral = Windows.Devices.Geolocation.Geolocator.requestAccessAsync().then(function (result) {
if (result === Windows.Devices.Geolocation.GeolocationAccessStatus.allowed) {
return loc;
}

return WinJS.Promise.wrapError({
code: PositionError.PERMISSION_DENIED,
message: 'Geolocation access has not been allowed by user.'
});
});
} else {
deferral = WinJS.Promise.wrap(loc);
}
} else {
deferral = WinJS.Promise.wrap(loc);
}

return deferral;
}

function createErrorCode() {
@@ -72,46 +92,45 @@ function createResult(pos) {

module.exports = {
getLocation: function (success, fail, args, env) {
ensureLocator();
if (loc != null)
{
var highAccuracy = args[0],
maxAge = args[1];

loc.desiredAccuracy = highAccuracy ?
Windows.Devices.Geolocation.PositionAccuracy.high :
Windows.Devices.Geolocation.PositionAccuracy.default;

loc.reportInterval = maxAge ? maxAge : 0;

loc.getGeopositionAsync().then(
function (pos) {
success(createResult(pos));
},
function (err) {
fail({
code: createErrorCode(),
message: err.message
});
}
);
}
else
{
fail({
code: PositionError.POSITION_UNAVAILABLE,
message: "You do not have the required location services present on your system."
});
}
ensureLocator().done(function () {
if (loc != null) {
var highAccuracy = args[0],
maxAge = args[1];

loc.desiredAccuracy = highAccuracy ?
Windows.Devices.Geolocation.PositionAccuracy.high :
Windows.Devices.Geolocation.PositionAccuracy.default;

loc.reportInterval = maxAge ? maxAge : 0;

loc.getGeopositionAsync().then(
function (pos) {
success(createResult(pos));
},
function (err) {
fail({
code: createErrorCode(),
message: err.message
});
}
);
}
else {
fail({
code: PositionError.POSITION_UNAVAILABLE,
message: "You do not have the required location services present on your system."
});
}
}, fail);
},

addWatch: function (success, fail, args, env) {
ensureLocator();
var clientId = args[0],
ensureLocator().done(function () {
var clientId = args[0],
highAccuracy = args[1],

onPositionChanged = function (e) {
success(createResult(e.position), {keepCallback: true});
success(createResult(e.position), { keepCallback: true });
},

onStatusChanged = function (e) {
@@ -138,22 +157,23 @@ module.exports = {
}
};

loc.desiredAccuracy = highAccuracy ?
Windows.Devices.Geolocation.PositionAccuracy.high :
Windows.Devices.Geolocation.PositionAccuracy.default;

if (cordova.platformId == 'windows' && WinJS.Utilities.isPhone) {
// on Windows Phone 8.1 'positionchanged' event fails with error below if movementThreshold is not set
// JavaScript runtime error: Operation aborted
// You must set the MovementThreshold property or the ReportInterval property before adding event handlers.
// WinRT information: You must set the MovementThreshold property or the ReportInterval property before adding event handlers
loc.movementThreshold = loc.movementThreshold || 1; // 1 meter
}

loc.addEventListener("positionchanged", onPositionChanged);
loc.addEventListener("statuschanged", onStatusChanged);

ids[clientId] = { pos: onPositionChanged, status: onStatusChanged };
loc.desiredAccuracy = highAccuracy ?
Windows.Devices.Geolocation.PositionAccuracy.high :
Windows.Devices.Geolocation.PositionAccuracy.default;

if (cordova.platformId == 'windows') {
// 'positionchanged' event fails with error below if movementThreshold is not set
// JavaScript runtime error: Operation aborted
// You must set the MovementThreshold property or the ReportInterval property before adding event handlers.
// WinRT information: You must set the MovementThreshold property or the ReportInterval property before adding event handlers
loc.movementThreshold = Number.EPSILON;
}

loc.addEventListener("positionchanged", onPositionChanged);
loc.addEventListener("statuschanged", onStatusChanged);

ids[clientId] = { pos: onPositionChanged, status: onStatusChanged };
}, fail);
},

clearWatch: function (success, fail, args, env) {

0 comments on commit f88dfd3

Please sign in to comment.