diff --git a/framework/assets/js/geolocation.js b/framework/assets/js/geolocation.js index 7d529d37..741730c8 100644 --- a/framework/assets/js/geolocation.js +++ b/framework/assets/js/geolocation.js @@ -8,12 +8,11 @@ function Geolocation() { */ this.lastPosition = null; this.lastError = null; - this.callbacks = { - onLocationChanged: [], - onError: [] - }; + this.listeners = null; }; +var geoListeners = []; + Geolocation.prototype.getCurrentPosition = function(successCallback, errorCallback, options) { var position = Geo.getCurrentLocation(); @@ -21,63 +20,6 @@ Geolocation.prototype.getCurrentPosition = function(successCallback, errorCallba this.fail = errorCallback; } -/** - * Asynchronously aquires the position repeatedly at a given interval. - * @param {Function} successCallback The function to call each time 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 and the frequency of the watch. - */ -Geolocation.prototype.watchPosition = function(successCallback, errorCallback, options) { - // 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; - - var that = this; - return setInterval(function() { - that.getCurrentPosition(successCallback, errorCallback, options); - }, frequency); -}; - - -/** - * Clears the specified position watch. - * @param {String} watchId The ID of the watch returned from #watchPosition. - */ -Geolocation.prototype.clearWatch = function(watchId) { - clearInterval(watchId); -}; - -/** - * Called by the geolocation framework when the current location is found. - * @param {PositionOptions} position The current position. - */ -Geolocation.prototype.setLocation = function(position) { - this.lastPosition = position; - for (var i = 0; i < this.callbacks.onLocationChanged.length; i++) { - var f = this.callbacks.onLocationChanged.shift(); - f(position); - } -}; - -/** - * Called by the geolocation framework when an error occurs while looking up the current position. - * @param {String} message The text of the error message. - */ -Geolocation.prototype.setError = function(message) { - this.lastError = message; - for (var i = 0; i < this.callbacks.onError.length; i++) { - var f = this.callbacks.onError.shift(); - f(message); - } -}; - // Run the global callback Geolocation.prototype.gotCurrentPosition = function(lat, lng, alt, altacc, head, vel, stamp) { @@ -103,16 +45,11 @@ Geolocation.prototype.gotCurrentPosition = function(lat, lng, alt, altacc, head, Geolocation.prototype.watchPosition = function(successCallback, errorCallback, options) { var frequency = (options != undefined)? options.frequency : 10000; - - if (!this.listeners) - { - this.listeners = []; - } - - var key = this.listeners.push( {"success" : successCallback, "fail" : errorCallback }) - 1; + + var key = geoListeners.push( {"success" : successCallback, "fail" : errorCallback }) - 1; // TO-DO: Get the names of the method and pass them as strings to the Java. - return Geolocation.start(frequency, key); + return Geo.start(frequency, key); } /* @@ -123,18 +60,19 @@ Geolocation.prototype.success = function(key, lat, lng, alt, altacc, head, vel, { var coords = new Coordinates(lat, lng, alt, altacc, head, vel); var loc = new Position(coords, stamp); - this.listeners[key].success(loc); + geoListeners[key].success(loc); } Geolocation.prototype.fail = function(key) { - this.listeners[key].fail(); + geoListeners[key].fail(); } 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) { @@ -147,4 +85,4 @@ PhoneGap.addConstructor(function() { ["setLocation", "getCurrentPosition", "watchPosition", "clearWatch", "setError", "start", "stop", "gotCurrentPosition"] ); -}); +}); \ No newline at end of file diff --git a/framework/src/com/phonegap/GeoBroker.java b/framework/src/com/phonegap/GeoBroker.java index d7cb5473..049bb2cc 100644 --- a/framework/src/com/phonegap/GeoBroker.java +++ b/framework/src/com/phonegap/GeoBroker.java @@ -21,6 +21,7 @@ public class GeoBroker { { mCtx = ctx; mAppView = view; + geoListeners = new HashMap(); } public void getCurrentLocation() diff --git a/framework/src/com/phonegap/GeoListener.java b/framework/src/com/phonegap/GeoListener.java index c9c8f3b0..613cbc15 100644 --- a/framework/src/com/phonegap/GeoListener.java +++ b/framework/src/com/phonegap/GeoListener.java @@ -47,7 +47,7 @@ public class GeoListener { params += "," + loc.getSpeed() + "," + loc.getTime(); if(id != "global") { - mAppView.loadUrl("javascript:navigator.geolocation.success(" + id + "," + params + ")"); + mAppView.loadUrl("javascript:navigator._geo.success(" + id + "," + params + ")"); } } @@ -55,11 +55,11 @@ public class GeoListener { { // Do we need to know why? How would we handle this? if (id != "global") { - mAppView.loadUrl("javascript:navigator.geolocation.fail(" + id + ")"); + mAppView.loadUrl("javascript:navigator._geo.fail(" + id + ")"); } else { - mAppView.loadUrl("javascript:navigator.geolocation.fail()"); + mAppView.loadUrl("javascript:navigator._geo.fail()"); } }