From 5255f632377c36beb0b4d2620940f33b6d02b2c7 Mon Sep 17 00:00:00 2001 From: filmaj Date: Sat, 13 Feb 2010 17:09:02 -0800 Subject: [PATCH] Fixed building of phonegap.js in build script. Fixed GPS on Android 2.0+ - I guess they updated the version of WebKit being used on Android (similar now to how it works on iPhone) and thus the browser has a native navigator.geolocation object. Employed Jesse`s approach to proxying an object`s method since we can`t directly overwrite it. --- droidgap | 4 +- framework/assets/js/geolocation.js | 76 ++++++++---------------------- 2 files changed, 21 insertions(+), 59 deletions(-) diff --git a/droidgap b/droidgap index 4efc8e92..86a7469c 100755 --- a/droidgap +++ b/droidgap @@ -75,9 +75,9 @@ class Build Dir.chdir("#{ @dir }framework#{ @s }assets#{ @s }js") basedir = "." js = Dir.new(basedir).entries - phonegapjs = "" + phonegapjs = IO.read('phonegap.js.base'); js.each do |script| - next if script[0].chr == "." + next if script[0].chr == "." or script == "phonegap.js.base" phonegapjs += IO.read(script) phonegapjs += "\n\n" end diff --git a/framework/assets/js/geolocation.js b/framework/assets/js/geolocation.js index e16fccfe..b9b4ac68 100644 --- a/framework/assets/js/geolocation.js +++ b/framework/assets/js/geolocation.js @@ -14,46 +14,12 @@ function Geolocation() { }; }; -/** - * 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 position data. - * @param {PositionOptions} options The options for getting the position data - * such as timeout. - */ -Geolocation.prototype.getCurrentPosition = function(successCallback, errorCallback, options) { - var referenceTime = 0; - if (this.lastPosition) - referenceTime = this.lastPosition.timeout; - else - this.start(options); - - var timeout = 20000; - var interval = 500; - if (typeof(options) == 'object' && options.interval) - interval = options.interval; - - if (typeof(successCallback) != 'function') - successCallback = function() {}; - if (typeof(errorCallback) != 'function') - errorCallback = function() {}; - - var dis = this; - var delay = 0; - var timer = setInterval(function() { - delay += interval; - - if (typeof(dis.lastPosition) == 'object' && dis.lastPosition.timestamp > referenceTime) { - successCallback(dis.lastPosition); - clearInterval(timer); - } else if (delay >= timeout) { - errorCallback(); - clearInterval(timer); - } - }, interval); -}; +Geolocation.prototype.getCurrentPosition = function(successCallback, errorCallback, options) +{ + var position = Geo.getCurrentLocation(); + this.global_success = successCallback; + this.fail = errorCallback; +} /** * Asynchronously aquires the position repeatedly at a given interval. @@ -111,23 +77,6 @@ Geolocation.prototype.setError = function(message) { f(message); } }; - -PhoneGap.addConstructor(function() { - if (typeof navigator.geolocation == "undefined") navigator.geolocation = new Geolocation(); -}); -/* -* Since we can't guarantee that we will have the most recent, we just try our best! -* -* Also, the API doesn't specify which version is the best version of the API -*/ - -Geolocation.prototype.getCurrentPosition = function(successCallback, errorCallback, options) -{ - var position = Geo.getCurrentLocation(); - this.global_success = successCallback; - this.fail = errorCallback; -} - // Run the global callback Geolocation.prototype.gotCurrentPosition = function(lat, lng, alt, altacc, head, vel, stamp) @@ -185,3 +134,16 @@ Geolocation.prototype.clearWatch = function(watchId) { Geo.stop(watchId); } +// Taken from Jesse's geo fix (similar problem) in PhoneGap iPhone. Go figure, same browser! +function __proxyObj(origObj, proxyObj, funkList) { + for (var v in funkList) { + origObj[funkList[v]] = proxyObj[funkList[v]]; + } +} +PhoneGap.addConstructor(function() { + navigator._geo = new Geolocation(); + __proxyObj(navigator.geolocation, navigator._geo, + ["setLocation", "getCurrentPosition", "watchPosition", + "clearWatch", "setError", "start", "stop", "gotCurrentPosition"] + ); +}); \ No newline at end of file