mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
updated location and location tests
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
import types = require("location/location_types");
|
||||
import appModule = require("application/application");
|
||||
import common = require("location/location_common");
|
||||
import merger = require("utils/module_merge");
|
||||
|
||||
// merge the exports of the types module with the exports of this file
|
||||
declare var exports;
|
||||
require("utils/module_merge").merge(types, exports);
|
||||
merger.merge(types, exports);
|
||||
merger.merge(common, exports);
|
||||
|
||||
export class LocationManager {
|
||||
// in meters
|
||||
|
||||
27
location/location.d.ts
vendored
27
location/location.d.ts
vendored
@@ -1,4 +1,7 @@
|
||||
export declare enum Accuracy {
|
||||
|
||||
import promises = require("promises/promises");
|
||||
|
||||
export declare enum Accuracy {
|
||||
// in meters
|
||||
ANY,
|
||||
HIGH,
|
||||
@@ -31,21 +34,31 @@ export declare class Location {
|
||||
public ios: any; // iOS CLLocation
|
||||
}
|
||||
|
||||
export declare class Options {
|
||||
export interface Options {
|
||||
/**
|
||||
* Specifies desired accuracy in meters. Defaults to DesiredAccuracy.HIGH
|
||||
*/
|
||||
desiredAccuracy: number;
|
||||
desiredAccuracy?: number;
|
||||
|
||||
/**
|
||||
* Update distance filter in meters. Specifies how often to update. Default on iOS is no filter, on Android it is 0 meters
|
||||
*/
|
||||
updateDistance: number;
|
||||
updateDistance?: number;
|
||||
|
||||
/**
|
||||
* Minimum time interval between location updates, in milliseconds (android only)
|
||||
* Minimum time interval between location updates, in milliseconds (ignored on iOS)
|
||||
*/
|
||||
minimumUpdateTime: number;
|
||||
minimumUpdateTime?: number;
|
||||
|
||||
/**
|
||||
* how old locations to receive in ms.
|
||||
*/
|
||||
maximumAge?: number;
|
||||
|
||||
/**
|
||||
* how long to wait for a location in ms.
|
||||
*/
|
||||
timeout?: number;
|
||||
}
|
||||
|
||||
export declare class LocationManager {
|
||||
@@ -98,3 +111,5 @@ export declare class LocationManager {
|
||||
*/
|
||||
lastKnownLocation: Location;
|
||||
}
|
||||
|
||||
export declare var getLocation: (options?: Options) => promises.Promise<Location>;
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import types = require("location/location_types");
|
||||
import common = require("location/location_common");
|
||||
import merger = require("utils/module_merge");
|
||||
|
||||
// merge the exports of the types module with the exports of this file
|
||||
declare var exports;
|
||||
require("utils/module_merge").merge(types, exports);
|
||||
merger.merge(types, exports);
|
||||
merger.merge(common, exports);
|
||||
|
||||
export class LocationManager {
|
||||
|
||||
|
||||
70
location/location_common.ts
Normal file
70
location/location_common.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
|
||||
import types = require("location/location_types");
|
||||
import promises = require("promises/promises");
|
||||
import locationModule = require("location/location");
|
||||
import timer = require("timer/timer");
|
||||
|
||||
export var getLocation = function (options?: types.Options) : promises.Promise<types.Location> {
|
||||
var d = promises.defer<types.Location>();
|
||||
|
||||
var timerId;
|
||||
var locationManager = new locationModule.LocationManager();
|
||||
|
||||
if (options && (0 === options.timeout)) {
|
||||
var location = locationManager.lastKnownLocation;
|
||||
if (location) {
|
||||
if (options && ("number" === typeof options.maximumAge)) {
|
||||
if (location.timestamp.valueOf() + options.maximumAge > new Date().valueOf()) {
|
||||
d.resolve(location);
|
||||
}
|
||||
else {
|
||||
d.reject(new Error("timeout is 0 and last known location is older than maximumAge"));
|
||||
}
|
||||
}
|
||||
else {
|
||||
d.resolve(location);
|
||||
}
|
||||
}
|
||||
else {
|
||||
d.reject(new Error("timeout is 0 and no known location found"));
|
||||
}
|
||||
return d.promise();
|
||||
}
|
||||
|
||||
locationManager.startLocationMonitoring(function (location: types.Location) {
|
||||
if (options && ("number" === typeof options.maximumAge)) {
|
||||
if (location.timestamp.valueOf() + options.maximumAge > new Date().valueOf()) {
|
||||
locationManager.stopLocationMonitoring();
|
||||
if ("undefined" !== typeof timerId) {
|
||||
timer.clearTimeout(timerId);
|
||||
}
|
||||
d.resolve(location);
|
||||
}
|
||||
}
|
||||
else {
|
||||
locationManager.stopLocationMonitoring();
|
||||
if ("undefined" !== typeof timerId) {
|
||||
timer.clearTimeout(timerId);
|
||||
}
|
||||
d.resolve(location);
|
||||
}
|
||||
}, function (error: Error) {
|
||||
console.error('Location error received: ' + error);
|
||||
locationManager.stopLocationMonitoring();
|
||||
if ("undefined" !== typeof timerId) {
|
||||
timer.clearTimeout(timerId);
|
||||
}
|
||||
d.reject(error);
|
||||
},
|
||||
options
|
||||
);
|
||||
|
||||
if (options && ("number" === typeof options.timeout)) {
|
||||
timerId = timer.setTimeout(function () {
|
||||
locationManager.stopLocationMonitoring();
|
||||
d.reject(new Error("timeout searching for location"));
|
||||
}, options.timeout);
|
||||
}
|
||||
|
||||
return d.promise();
|
||||
}
|
||||
@@ -23,21 +23,31 @@ export class Location {
|
||||
public ios: any; // iOS native location
|
||||
}
|
||||
|
||||
export class Options {
|
||||
export interface Options {
|
||||
/**
|
||||
* Specifies desired accuracy in meters. Defaults to DesiredAccuracy.HIGH
|
||||
*/
|
||||
public desiredAccuracy: number;
|
||||
desiredAccuracy?: number;
|
||||
|
||||
/**
|
||||
* Update distance filter in meters. Specifies how often to update. Default on iOS is no filter, on Android it is 0 meters
|
||||
*/
|
||||
public updateDistance: number;
|
||||
updateDistance?: number;
|
||||
|
||||
/**
|
||||
* Minimum time interval between location updates, in milliseconds (ignored on iOS)
|
||||
*/
|
||||
public minimumUpdateTime: number;
|
||||
minimumUpdateTime?: number;
|
||||
|
||||
/**
|
||||
* how old locations to receive in ms.
|
||||
*/
|
||||
maximumAge?: number;
|
||||
|
||||
/**
|
||||
* how long to wait for a location in ms.
|
||||
*/
|
||||
timeout?: number;
|
||||
}
|
||||
|
||||
export class LocationRegion {
|
||||
|
||||
Reference in New Issue
Block a user