2010-10-19 04:31:16 +08:00
|
|
|
/*
|
|
|
|
* PhoneGap is available under *either* the terms of the modified BSD license *or* the
|
|
|
|
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005-2010, Nitobi Software Inc.
|
2011-05-21 01:50:20 +08:00
|
|
|
* Copyright (c) 2010-2011, IBM Corporation
|
2010-10-19 04:31:16 +08:00
|
|
|
*/
|
|
|
|
|
2011-03-31 02:29:24 +08:00
|
|
|
if (!PhoneGap.hasResource("geolocation")) {
|
|
|
|
PhoneGap.addResource("geolocation");
|
|
|
|
|
2009-11-18 02:38:49 +08:00
|
|
|
/**
|
|
|
|
* This class provides access to device GPS data.
|
|
|
|
* @constructor
|
|
|
|
*/
|
2011-05-21 01:50:20 +08:00
|
|
|
var Geolocation = function() {
|
2010-09-23 03:47:52 +08:00
|
|
|
|
|
|
|
// The last known GPS position.
|
2009-11-18 02:38:49 +08:00
|
|
|
this.lastPosition = null;
|
2010-09-23 03:47:52 +08:00
|
|
|
|
|
|
|
// Geolocation listeners
|
|
|
|
this.listeners = {};
|
2011-03-31 02:29:24 +08:00
|
|
|
};
|
2009-11-18 02:38:49 +08:00
|
|
|
|
2010-09-23 03:47:52 +08:00
|
|
|
/**
|
|
|
|
* Position error object
|
|
|
|
*
|
2011-03-31 00:14:21 +08:00
|
|
|
* @constructor
|
2010-09-23 03:47:52 +08:00
|
|
|
* @param code
|
|
|
|
* @param message
|
|
|
|
*/
|
2011-05-21 01:50:20 +08:00
|
|
|
var PositionError = function(code, message) {
|
2010-09-23 03:47:52 +08:00
|
|
|
this.code = code;
|
|
|
|
this.message = message;
|
2011-03-31 02:29:24 +08:00
|
|
|
};
|
2010-03-16 08:01:12 +08:00
|
|
|
|
2010-09-23 03:47:52 +08:00
|
|
|
PositionError.PERMISSION_DENIED = 1;
|
|
|
|
PositionError.POSITION_UNAVAILABLE = 2;
|
|
|
|
PositionError.TIMEOUT = 3;
|
2009-11-18 02:38:49 +08:00
|
|
|
|
2010-09-23 03:47:52 +08:00
|
|
|
/**
|
|
|
|
* Asynchronously aquires the current position.
|
|
|
|
*
|
|
|
|
* @param {Function} successCallback The function to call when the position data is available
|
|
|
|
* @param {Function} errorCallback The function to call when there is an error getting the heading position. (OPTIONAL)
|
|
|
|
* @param {PositionOptions} options The options for getting the position data. (OPTIONAL)
|
|
|
|
*/
|
|
|
|
Geolocation.prototype.getCurrentPosition = function(successCallback, errorCallback, options) {
|
2011-02-03 00:46:19 +08:00
|
|
|
if (navigator._geo.listeners.global) {
|
2010-09-23 03:47:52 +08:00
|
|
|
console.log("Geolocation Error: Still waiting for previous getCurrentPosition() request.");
|
|
|
|
try {
|
|
|
|
errorCallback(new PositionError(PositionError.TIMEOUT, "Geolocation Error: Still waiting for previous getCurrentPosition() request."));
|
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2010-09-24 03:34:56 +08:00
|
|
|
var maximumAge = 10000;
|
|
|
|
var enableHighAccuracy = false;
|
|
|
|
var timeout = 10000;
|
2011-02-03 00:46:19 +08:00
|
|
|
if (typeof options !== "undefined") {
|
|
|
|
if (typeof options.maximumAge !== "undefined") {
|
2010-09-24 03:34:56 +08:00
|
|
|
maximumAge = options.maximumAge;
|
|
|
|
}
|
2011-02-03 00:46:19 +08:00
|
|
|
if (typeof options.enableHighAccuracy !== "undefined") {
|
2010-09-24 03:34:56 +08:00
|
|
|
enableHighAccuracy = options.enableHighAccuracy;
|
|
|
|
}
|
2011-02-03 00:46:19 +08:00
|
|
|
if (typeof options.timeout !== "undefined") {
|
2010-09-24 03:34:56 +08:00
|
|
|
timeout = options.timeout;
|
|
|
|
}
|
|
|
|
}
|
2011-02-03 00:46:19 +08:00
|
|
|
navigator._geo.listeners.global = {"success" : successCallback, "fail" : errorCallback };
|
2010-10-21 12:53:33 +08:00
|
|
|
PhoneGap.exec(null, null, "Geolocation", "getCurrentLocation", [enableHighAccuracy, timeout, maximumAge]);
|
2011-02-03 00:46:19 +08:00
|
|
|
};
|
2009-11-18 02:38:49 +08:00
|
|
|
|
2010-09-23 03:47:52 +08:00
|
|
|
/**
|
|
|
|
* Asynchronously watches the geolocation for changes to geolocation. When a change occurs,
|
|
|
|
* the successCallback is called with the new location.
|
|
|
|
*
|
|
|
|
* @param {Function} successCallback The function to call each time the location data is available
|
|
|
|
* @param {Function} errorCallback The function to call when there is an error getting the location data. (OPTIONAL)
|
|
|
|
* @param {PositionOptions} options The options for getting the location data such as frequency. (OPTIONAL)
|
|
|
|
* @return String The watch id that must be passed to #clearWatch to stop watching.
|
|
|
|
*/
|
|
|
|
Geolocation.prototype.watchPosition = function(successCallback, errorCallback, options) {
|
2010-09-24 03:34:56 +08:00
|
|
|
var maximumAge = 10000;
|
|
|
|
var enableHighAccuracy = false;
|
|
|
|
var timeout = 10000;
|
2011-02-03 00:46:19 +08:00
|
|
|
if (typeof options !== "undefined") {
|
|
|
|
if (typeof options.frequency !== "undefined") {
|
2010-09-24 03:34:56 +08:00
|
|
|
maximumAge = options.frequency;
|
|
|
|
}
|
2011-02-03 00:46:19 +08:00
|
|
|
if (typeof options.maximumAge !== "undefined") {
|
2010-09-24 03:34:56 +08:00
|
|
|
maximumAge = options.maximumAge;
|
|
|
|
}
|
2011-02-03 00:46:19 +08:00
|
|
|
if (typeof options.enableHighAccuracy !== "undefined") {
|
2010-09-24 03:34:56 +08:00
|
|
|
enableHighAccuracy = options.enableHighAccuracy;
|
|
|
|
}
|
2011-02-03 00:46:19 +08:00
|
|
|
if (typeof options.timeout !== "undefined") {
|
2010-09-24 03:34:56 +08:00
|
|
|
timeout = options.timeout;
|
|
|
|
}
|
|
|
|
}
|
2010-09-23 03:47:52 +08:00
|
|
|
var id = PhoneGap.createUUID();
|
|
|
|
navigator._geo.listeners[id] = {"success" : successCallback, "fail" : errorCallback };
|
2010-10-21 12:53:33 +08:00
|
|
|
PhoneGap.exec(null, null, "Geolocation", "start", [id, enableHighAccuracy, timeout, maximumAge]);
|
2010-09-23 03:47:52 +08:00
|
|
|
return id;
|
|
|
|
};
|
|
|
|
|
2009-11-18 02:38:49 +08:00
|
|
|
/*
|
2010-09-23 03:47:52 +08:00
|
|
|
* Native callback when watch position has a new position.
|
2010-09-24 03:34:56 +08:00
|
|
|
* PRIVATE METHOD
|
2009-11-18 02:38:49 +08:00
|
|
|
*
|
2010-09-23 03:47:52 +08:00
|
|
|
* @param {String} id
|
|
|
|
* @param {Number} lat
|
|
|
|
* @param {Number} lng
|
|
|
|
* @param {Number} alt
|
|
|
|
* @param {Number} altacc
|
|
|
|
* @param {Number} head
|
|
|
|
* @param {Number} vel
|
|
|
|
* @param {Number} stamp
|
2009-11-18 02:38:49 +08:00
|
|
|
*/
|
2010-09-23 03:47:52 +08:00
|
|
|
Geolocation.prototype.success = function(id, lat, lng, alt, altacc, head, vel, stamp) {
|
|
|
|
var coords = new Coordinates(lat, lng, alt, altacc, head, vel);
|
|
|
|
var loc = new Position(coords, stamp);
|
|
|
|
try {
|
2011-02-03 00:46:19 +08:00
|
|
|
if (lat === "undefined" || lng === "undefined") {
|
2010-09-23 03:47:52 +08:00
|
|
|
navigator._geo.listeners[id].fail(new PositionError(PositionError.POSITION_UNAVAILABLE, "Lat/Lng are undefined."));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
navigator._geo.lastPosition = loc;
|
|
|
|
navigator._geo.listeners[id].success(loc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
console.log("Geolocation Error: Error calling success callback function.");
|
|
|
|
}
|
2009-11-18 02:38:49 +08:00
|
|
|
|
2011-02-03 00:46:19 +08:00
|
|
|
if (id === "global") {
|
|
|
|
delete navigator._geo.listeners.global;
|
2010-09-23 03:47:52 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Native callback when watch position has an error.
|
2010-09-24 03:34:56 +08:00
|
|
|
* PRIVATE METHOD
|
2010-09-23 03:47:52 +08:00
|
|
|
*
|
|
|
|
* @param {String} id The ID of the watch
|
|
|
|
* @param {Number} code The error code
|
|
|
|
* @param {String} msg The error message
|
|
|
|
*/
|
|
|
|
Geolocation.prototype.fail = function(id, code, msg) {
|
|
|
|
try {
|
|
|
|
navigator._geo.listeners[id].fail(new PositionError(code, msg));
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
console.log("Geolocation Error: Error calling error callback function.");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears the specified heading watch.
|
|
|
|
*
|
|
|
|
* @param {String} id The ID of the watch returned from #watchPosition
|
|
|
|
*/
|
|
|
|
Geolocation.prototype.clearWatch = function(id) {
|
2010-10-21 12:53:33 +08:00
|
|
|
PhoneGap.exec(null, null, "Geolocation", "stop", [id]);
|
2010-09-23 03:47:52 +08:00
|
|
|
delete navigator._geo.listeners[id];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Force the PhoneGap geolocation to be used instead of built-in.
|
|
|
|
*/
|
|
|
|
Geolocation.usingPhoneGap = false;
|
|
|
|
Geolocation.usePhoneGap = function() {
|
|
|
|
if (Geolocation.usingPhoneGap) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Geolocation.usingPhoneGap = true;
|
|
|
|
|
|
|
|
// Set built-in geolocation methods to our own implementations
|
|
|
|
// (Cannot replace entire geolocation, but can replace individual methods)
|
|
|
|
navigator.geolocation.setLocation = navigator._geo.setLocation;
|
|
|
|
navigator.geolocation.getCurrentPosition = navigator._geo.getCurrentPosition;
|
|
|
|
navigator.geolocation.watchPosition = navigator._geo.watchPosition;
|
|
|
|
navigator.geolocation.clearWatch = navigator._geo.clearWatch;
|
|
|
|
navigator.geolocation.start = navigator._geo.start;
|
|
|
|
navigator.geolocation.stop = navigator._geo.stop;
|
|
|
|
};
|
2010-03-16 08:01:12 +08:00
|
|
|
|
2010-02-14 09:09:02 +08:00
|
|
|
PhoneGap.addConstructor(function() {
|
2010-09-23 03:47:52 +08:00
|
|
|
navigator._geo = new Geolocation();
|
|
|
|
|
|
|
|
// No native geolocation object for Android 1.x, so use PhoneGap geolocation
|
2011-02-03 00:46:19 +08:00
|
|
|
if (typeof navigator.geolocation === 'undefined') {
|
2010-09-23 03:47:52 +08:00
|
|
|
navigator.geolocation = navigator._geo;
|
|
|
|
Geolocation.usingPhoneGap = true;
|
|
|
|
}
|
2010-07-16 07:55:00 +08:00
|
|
|
});
|
2011-05-21 01:50:20 +08:00
|
|
|
}
|