mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-01 02:12:58 +08:00
Add geolocation options as defined by W3C spec.
This commit is contained in:
parent
edfa41c9f9
commit
7f7cc1db2a
@ -42,8 +42,22 @@ Geolocation.prototype.getCurrentPosition = function(successCallback, errorCallba
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
var maximumAge = 10000;
|
||||||
|
var enableHighAccuracy = false;
|
||||||
|
var timeout = 10000;
|
||||||
|
if (typeof options != "undefined") {
|
||||||
|
if (typeof options.maximumAge != "undefined") {
|
||||||
|
maximumAge = options.maximumAge;
|
||||||
|
}
|
||||||
|
if (typeof options.enableHighAccuracy != "undefined") {
|
||||||
|
enableHighAccuracy = options.enableHighAccuracy;
|
||||||
|
}
|
||||||
|
if (typeof options.timeout != "undefined") {
|
||||||
|
timeout = options.timeout;
|
||||||
|
}
|
||||||
|
}
|
||||||
navigator._geo.listeners["global"] = {"success" : successCallback, "fail" : errorCallback };
|
navigator._geo.listeners["global"] = {"success" : successCallback, "fail" : errorCallback };
|
||||||
PhoneGap.execAsync(null, null, "Geolocation", "getCurrentLocation", []);
|
PhoneGap.execAsync(null, null, "Geolocation", "getCurrentLocation", [enableHighAccuracy, timeout, maximumAge]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -56,15 +70,32 @@ Geolocation.prototype.getCurrentPosition = function(successCallback, errorCallba
|
|||||||
* @return String The watch id that must be passed to #clearWatch to stop watching.
|
* @return String The watch id that must be passed to #clearWatch to stop watching.
|
||||||
*/
|
*/
|
||||||
Geolocation.prototype.watchPosition = function(successCallback, errorCallback, options) {
|
Geolocation.prototype.watchPosition = function(successCallback, errorCallback, options) {
|
||||||
var frequency = (options != undefined)? options.frequency : 10000;
|
var maximumAge = 10000;
|
||||||
|
var enableHighAccuracy = false;
|
||||||
|
var timeout = 10000;
|
||||||
|
if (typeof options != "undefined") {
|
||||||
|
if (typeof options.frequency != "undefined") {
|
||||||
|
maximumAge = options.frequency;
|
||||||
|
}
|
||||||
|
if (typeof options.maximumAge != "undefined") {
|
||||||
|
maximumAge = options.maximumAge;
|
||||||
|
}
|
||||||
|
if (typeof options.enableHighAccuracy != "undefined") {
|
||||||
|
enableHighAccuracy = options.enableHighAccuracy;
|
||||||
|
}
|
||||||
|
if (typeof options.timeout != "undefined") {
|
||||||
|
timeout = options.timeout;
|
||||||
|
}
|
||||||
|
}
|
||||||
var id = PhoneGap.createUUID();
|
var id = PhoneGap.createUUID();
|
||||||
navigator._geo.listeners[id] = {"success" : successCallback, "fail" : errorCallback };
|
navigator._geo.listeners[id] = {"success" : successCallback, "fail" : errorCallback };
|
||||||
PhoneGap.execAsync(null, null, "Geolocation", "start", [frequency, id]);
|
PhoneGap.execAsync(null, null, "Geolocation", "start", [id, enableHighAccuracy, timeout, maximumAge]);
|
||||||
return id;
|
return id;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Native callback when watch position has a new position.
|
* Native callback when watch position has a new position.
|
||||||
|
* PRIVATE METHOD
|
||||||
*
|
*
|
||||||
* @param {String} id
|
* @param {String} id
|
||||||
* @param {Number} lat
|
* @param {Number} lat
|
||||||
@ -98,6 +129,7 @@ Geolocation.prototype.success = function(id, lat, lng, alt, altacc, head, vel, s
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Native callback when watch position has an error.
|
* Native callback when watch position has an error.
|
||||||
|
* PRIVATE METHOD
|
||||||
*
|
*
|
||||||
* @param {String} id The ID of the watch
|
* @param {String} id The ID of the watch
|
||||||
* @param {Number} code The error code
|
* @param {Number} code The error code
|
||||||
|
@ -67,10 +67,10 @@ public class GeoBroker implements Plugin {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
if (action.equals("getCurrentLocation")) {
|
if (action.equals("getCurrentLocation")) {
|
||||||
this.getCurrentLocation();
|
this.getCurrentLocation(args.getBoolean(0), args.getInt(1), args.getInt(2));
|
||||||
}
|
}
|
||||||
else if (action.equals("start")) {
|
else if (action.equals("start")) {
|
||||||
String s = this.start(args.getInt(0), args.getString(1));
|
String s = this.start(args.getString(0), args.getBoolean(1), args.getInt(2), args.getInt(3));
|
||||||
return new PluginResult(status, s);
|
return new PluginResult(status, s);
|
||||||
}
|
}
|
||||||
else if (action.equals("stop")) {
|
else if (action.equals("stop")) {
|
||||||
@ -143,36 +143,42 @@ public class GeoBroker implements Plugin {
|
|||||||
/**
|
/**
|
||||||
* Get current location.
|
* Get current location.
|
||||||
* The result is returned to JavaScript via a callback.
|
* The result is returned to JavaScript via a callback.
|
||||||
|
*
|
||||||
|
* @param enableHighAccuracy
|
||||||
|
* @param timeout
|
||||||
|
* @param maximumAge
|
||||||
*/
|
*/
|
||||||
public void getCurrentLocation() {
|
public void getCurrentLocation(boolean enableHighAccuracy, int timeout, int maximumAge) {
|
||||||
|
|
||||||
// Create a geolocation listener just for getCurrentLocation and call it "global"
|
// Create a geolocation listener just for getCurrentLocation and call it "global"
|
||||||
if (this.global == null) {
|
if (this.global == null) {
|
||||||
this.global = new GeoListener("global", this.ctx, 10000, this.webView);
|
this.global = new GeoListener("global", this.ctx, maximumAge, this.webView);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.global.start(10000);
|
this.global.start(maximumAge);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start geolocation listener and add to listener list.
|
* Start geolocation listener and add to listener list.
|
||||||
*
|
*
|
||||||
* @param freq Period to retrieve geolocation
|
* @param key The listener id
|
||||||
* @param key The listener id
|
* @param enableHighAccuracy
|
||||||
|
* @param timeout
|
||||||
|
* @param maximumAge
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public String start(int freq, String key) {
|
public String start(String key, boolean enableHighAccuracy, int timeout, int maximumAge) {
|
||||||
|
|
||||||
// Make sure this listener doesn't already exist
|
// Make sure this listener doesn't already exist
|
||||||
GeoListener listener = geoListeners.get(key);
|
GeoListener listener = geoListeners.get(key);
|
||||||
if (listener == null) {
|
if (listener == null) {
|
||||||
listener = new GeoListener(key, this.ctx, freq, this.webView);
|
listener = new GeoListener(key, this.ctx, maximumAge, this.webView);
|
||||||
geoListeners.put(key, listener);
|
geoListeners.put(key, listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start it
|
// Start it
|
||||||
listener.start(freq);
|
listener.start(maximumAge);
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user