Skip to content

Commit

Permalink
getCurrentPosition was starting the tracking, and not stopping, for j…
Browse files Browse the repository at this point in the history
…ust one call. changed it to use mojo.getCurrentPosition rather than startTracking
  • Loading branch information
Ryan Willoughby committed Aug 5, 2010
1 parent 64ea52d commit 6b92a55
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 30 deletions.
116 changes: 101 additions & 15 deletions framework/www/phonegap.js
Expand Up @@ -521,22 +521,24 @@ function Geolocation() {
* such as timeout.
*/
Geolocation.prototype.getCurrentPosition = function(successCallback, errorCallback, options) {
var referenceTime = 0;
/*
var referenceTime = 0;
if (this.lastPosition)
referenceTime = this.lastPosition.timestamp;
else
this.start(options);

*/

var timeout = 20000;
var interval = 500;
if (typeof(options) == 'object' && options.interval)
interval = options.interval;

if (typeof(options) == 'object' && options.timeout)
timeout = options.timeout;

if (typeof(successCallback) != 'function')
successCallback = function() {};
if (typeof(errorCallback) != 'function')
errorCallback = function() {};

/*
var dis = this;
var delay = 0;
var timer = setInterval(function() {
Expand All @@ -552,6 +554,56 @@ Geolocation.prototype.getCurrentPosition = function(successCallback, errorCallba
}
//else the interval gets called again
}, interval);
*/

var responseTime;
if (timeout <=5000)
responseTime = 1;
else if (5000 < timeout <= 20000)
responseTime = 2;
else
responseTime = 3;

var timer = setTimeout(function(){
errorCallback({ message: "timeout" });
}, timeout);

var startTime = (new Date()).getTime();

var alias = this;

// It may be that getCurrentPosition is less reliable than startTracking ... but
// not sure if we want to be starting and stopping the tracker if we're not watching.
new Mojo.Service.Request('palm://com.palm.location', {
method:"getCurrentPosition",
parameters:{
responseTime: responseTime
},
onSuccess: function(event) {
alias.lastPosition = {
coords: {
latitude: event.latitude,
longitude: event.longitude,
altitude: (event.altitude >= 0 ? event.altitude : null),
speed: (event.velocity >= 0 ? event.velocity : null),
heading: (event.heading >= 0 ? event.heading : null),
accuracy: (event.horizAccuracy >= 0 ? event.horizAccuracy : null)
},
timestamp: new Date().getTime()
};

var responseTime = alias.lastPosition.timestamp - startTime;
if (responseTime <= timeout)
{
clearTimeout(timer);
successCallback(alias.lastPosition);
}
},
onFailure: function() {
errorCallback();
}
});

};

/*
Expand All @@ -567,14 +619,24 @@ Geolocation.prototype.watchPosition = function(successCallback, errorCallback, o
// Invoke the appropriate callback with a new Position object every time the implementation
// determines that the position of the hosting device has changed.

this.getCurrentPosition(successCallback, errorCallback, options);
var frequency = 10000;
if (typeof(options) == 'object' && options.frequency)
frequency = options.frequency;
if (typeof(options) == 'object' && options.frequency)
frequency = options.frequency;

this.start(options, errorCallback);

var that = this;
var referenceTime = 0;
if (this.lastPosition)
referenceTime = this.lastPosition.timestamp;

var alias = this;
return setInterval(function() {
that.getCurrentPosition(successCallback, errorCallback, options);
// check if we have a new position, if so call our successcallback
if (!alias.lastPosition)
return;

if (alias.lastPosition.timestamp > referenceTime)
successCallback(alias.lastPosition);
}, frequency);
};

Expand All @@ -585,27 +647,51 @@ Geolocation.prototype.watchPosition = function(successCallback, errorCallback, o
*/
Geolocation.prototype.clearWatch = function(watchId) {
clearInterval(watchId);
this.stop();
};

Geolocation.prototype.start = function(options) {
Geolocation.prototype.start = function(options, errorCallback) {
//options.timeout;
//options.interval;

if (typeof(errorCallback) != 'function')
errorCallback = function(){};

var that = this;
var frequency = 10000;
if (typeof(options) == 'object' && options.frequency)
frequency = options.frequency;

var responseTime;
if (frequency <=5000)
responseTime = 1;
else if (5000 < options.frequency <= 20000)
responseTime = 2;
else
responseTime = 3;

//location tracking does not support setting a custom interval :P
this.trackingHandle = new Mojo.Service.Request('palm://com.palm.location', {
method : 'startTracking',
parameters: {
subscribe: true
},
},
onSuccess: function(event) {
that.lastPosition = {
coords: { latitude: event.latitude, longitude: event.longitude, altitude: event.altitude, speed: event.velocity, heading: event.heading, accuracy: event.horizAccuracy },
coords: {
latitude: event.latitude,
longitude: event.longitude,
altitude: (event.altitude >= 0 ? event.altitude : null),
speed: (event.velocity >= 0 ? event.velocity : null),
heading: (event.heading >= 0 ? event.heading : null),
accuracy: (event.horizAccuracy >= 0 ? event.horizAccuracy : null)
},
timestamp: new Date().getTime()
};
},
onFailure: function() {}
onFailure: function() {
errorCallback();
}
});
};

Expand Down
116 changes: 101 additions & 15 deletions js/geolocation.js
Expand Up @@ -25,22 +25,24 @@ function Geolocation() {
* such as timeout.
*/
Geolocation.prototype.getCurrentPosition = function(successCallback, errorCallback, options) {
var referenceTime = 0;
/*
var referenceTime = 0;
if (this.lastPosition)
referenceTime = this.lastPosition.timestamp;
else
this.start(options);

*/

var timeout = 20000;
var interval = 500;
if (typeof(options) == 'object' && options.interval)
interval = options.interval;

if (typeof(options) == 'object' && options.timeout)
timeout = options.timeout;

if (typeof(successCallback) != 'function')
successCallback = function() {};
if (typeof(errorCallback) != 'function')
errorCallback = function() {};

/*
var dis = this;
var delay = 0;
var timer = setInterval(function() {
Expand All @@ -56,6 +58,56 @@ Geolocation.prototype.getCurrentPosition = function(successCallback, errorCallba
}
//else the interval gets called again
}, interval);
*/

var responseTime;
if (timeout <=5000)
responseTime = 1;
else if (5000 < timeout <= 20000)
responseTime = 2;
else
responseTime = 3;

var timer = setTimeout(function(){
errorCallback({ message: "timeout" });
}, timeout);

var startTime = (new Date()).getTime();

var alias = this;

// It may be that getCurrentPosition is less reliable than startTracking ... but
// not sure if we want to be starting and stopping the tracker if we're not watching.
new Mojo.Service.Request('palm://com.palm.location', {
method:"getCurrentPosition",
parameters:{
responseTime: responseTime
},
onSuccess: function(event) {
alias.lastPosition = {
coords: {
latitude: event.latitude,
longitude: event.longitude,
altitude: (event.altitude >= 0 ? event.altitude : null),
speed: (event.velocity >= 0 ? event.velocity : null),
heading: (event.heading >= 0 ? event.heading : null),
accuracy: (event.horizAccuracy >= 0 ? event.horizAccuracy : null)
},
timestamp: new Date().getTime()
};

var responseTime = alias.lastPosition.timestamp - startTime;
if (responseTime <= timeout)
{
clearTimeout(timer);
successCallback(alias.lastPosition);
}
},
onFailure: function() {
errorCallback();
}
});

};

/*
Expand All @@ -71,14 +123,24 @@ Geolocation.prototype.watchPosition = function(successCallback, errorCallback, o
// Invoke the appropriate callback with a new Position object every time the implementation
// determines that the position of the hosting device has changed.

this.getCurrentPosition(successCallback, errorCallback, options);
var frequency = 10000;
if (typeof(options) == 'object' && options.frequency)
frequency = options.frequency;
if (typeof(options) == 'object' && options.frequency)
frequency = options.frequency;

this.start(options, errorCallback);

var that = this;
var referenceTime = 0;
if (this.lastPosition)
referenceTime = this.lastPosition.timestamp;

var alias = this;
return setInterval(function() {
that.getCurrentPosition(successCallback, errorCallback, options);
// check if we have a new position, if so call our successcallback
if (!alias.lastPosition)
return;

if (alias.lastPosition.timestamp > referenceTime)
successCallback(alias.lastPosition);
}, frequency);
};

Expand All @@ -89,27 +151,51 @@ Geolocation.prototype.watchPosition = function(successCallback, errorCallback, o
*/
Geolocation.prototype.clearWatch = function(watchId) {
clearInterval(watchId);
this.stop();
};

Geolocation.prototype.start = function(options) {
Geolocation.prototype.start = function(options, errorCallback) {
//options.timeout;
//options.interval;

if (typeof(errorCallback) != 'function')
errorCallback = function(){};

var that = this;
var frequency = 10000;
if (typeof(options) == 'object' && options.frequency)
frequency = options.frequency;

var responseTime;
if (frequency <=5000)
responseTime = 1;
else if (5000 < options.frequency <= 20000)
responseTime = 2;
else
responseTime = 3;

//location tracking does not support setting a custom interval :P
this.trackingHandle = new Mojo.Service.Request('palm://com.palm.location', {
method : 'startTracking',
parameters: {
subscribe: true
},
},
onSuccess: function(event) {
that.lastPosition = {
coords: { latitude: event.latitude, longitude: event.longitude, altitude: event.altitude, speed: event.velocity, heading: event.heading, accuracy: event.horizAccuracy },
coords: {
latitude: event.latitude,
longitude: event.longitude,
altitude: (event.altitude >= 0 ? event.altitude : null),
speed: (event.velocity >= 0 ? event.velocity : null),
heading: (event.heading >= 0 ? event.heading : null),
accuracy: (event.horizAccuracy >= 0 ? event.horizAccuracy : null)
},
timestamp: new Date().getTime()
};
},
onFailure: function() {}
onFailure: function() {
errorCallback();
}
});
};

Expand Down

0 comments on commit 6b92a55

Please sign in to comment.