Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,18 @@ module.exports = function(grunt) {
copy: {
package: {
files: [
{ expand: true, cwd: 'source', src: ['**/*.js', 'package.json', 'README.md', 'platforms/**'], dest: 'dist/package' }
{
expand: true,
cwd: 'source',
src: [
'**/*.js',
'**/*.d.ts',
'package.json',
'README.md',
'platforms/**'
],
dest: 'dist/package'
}
]
}
},
Expand Down
2 changes: 2 additions & 0 deletions source/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./location";
export * from "./location-monitor";
46 changes: 46 additions & 0 deletions source/location-monitor.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {Location} from "./location";

/**
* Provides options for location monitoring.
*/
export interface Options {
/**
* Specifies desired accuracy in meters. Defaults to DesiredAccuracy.HIGH
*/
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;

/**
* Minimum time interval between location updates, in milliseconds (ignored on iOS)
*/
minimumUpdateTime?: number;

/**
* how old locations to receive in ms.
*/
maximumAge?: number;

/**
* how long to wait for a location in ms.
*/
timeout?: number;
}

export function getCurrentLocation(options): Promise<Location>;
export function watchLocation(options): Number;
export function clearWatch(watchId): void;
export function enableLocationRequest(always?: boolean): void;
export function isEnabled(): boolean;
export function distance(loc1: Location, loc2: Location): number;

export class LocationMonitor {
static getLastKnownLocation(): Location;
static stopLocationMonitoring(locListenerId: Number): void;
static startLocationMonitoring(options: Options, locListener: any): void;
static createListenerWithCallbackAndOptions(successCallback: (location: Location) => void, options: Options): any;
static stopLocationMonitoring(locListenerId: Number): void;
}
54 changes: 54 additions & 0 deletions source/location.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* A data class that encapsulates common properties for a geolocation.
*/
export class Location {
/**
* The latitude of the geolocation, in degrees.
*/
latitude: number;

/**
* The longitude of the geolocation, in degrees.
*/
longitude: number;

/**
* The altitude (if available), in meters above sea level.
*/
altitude: number;

/**
* The horizontal accuracy, in meters.
*/
horizontalAccuracy: number;

/**
* The vertical accuracy, in meters.
*/
verticalAccuracy: number;

/**
* The speed, in meters/second over ground.
*/
speed: number;

/**
* The direction (course), in degrees.
*/
direction: number;

/**
* The time at which this location was determined.
*/
timestamp: Date;

/**
* The android-specific [location](http://developer.android.com/reference/android/location/Location.html) object.
*/
android: any;

/**
* The ios-specific [CLLocation](https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocation_Class/) object.
*/
ios: any;
}
23 changes: 14 additions & 9 deletions source/nativescript-geolocation-common.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import defModule = require("nativescript-geolocation");
import {Location as LocationDef} from "./location";
import * as locationModule from "./location-monitor";
import timer = require("timer");
var location: typeof locationModule = null; //required dynamically

export class Location implements defModule.Location {
export class Location implements LocationDef {
public latitude: number;
public longitude: number;

Expand All @@ -25,11 +27,14 @@ var defaultGetLocationTimeout = 5 * 60 * 1000; // 5 minutes
// options - desiredAccuracy, updateDistance, minimumUpdateTime, maximumAge, timeout
export function getCurrentLocation(options) {
options = options || {};
if (!location) {
location = require("nativescript-geolocation");
}

if (options && options.timeout === 0) {
// we should take any cached location e.g. lastKnownLocation
return new Promise(function (resolve, reject) {
var lastLocation = defModule.LocationMonitor.getLastKnownLocation();
var lastLocation = location.LocationMonitor.getLastKnownLocation();
if (lastLocation) {
if (options && typeof options.maximumAge === "number") {
if (lastLocation.timestamp.valueOf() + options.maximumAge > new Date().valueOf()) {
Expand All @@ -54,9 +59,9 @@ export function getCurrentLocation(options) {
if (timerId !== undefined) {
timer.clearTimeout(timerId);
}
defModule.LocationMonitor.stopLocationMonitoring(locListenerId);
location.LocationMonitor.stopLocationMonitoring(locListenerId);
}
if (!defModule.isEnabled()) {
if (!location.isEnabled()) {
reject(new Error("Location service is disabled"));
}
var successCallback = function(location) {
Expand All @@ -75,10 +80,10 @@ export function getCurrentLocation(options) {
resolve(location);
}
};
var locListener = defModule.LocationMonitor.createListenerWithCallbackAndOptions(successCallback, options);
var locListener = location.LocationMonitor.createListenerWithCallbackAndOptions(successCallback, options);

try {
defModule.LocationMonitor.startLocationMonitoring(options, locListener);
location.LocationMonitor.startLocationMonitoring(options, locListener);
}
catch (e) {
stopTimerAndMonitor((<any>locListener).id);
Expand All @@ -87,9 +92,9 @@ export function getCurrentLocation(options) {

if (options && typeof options.timeout === "number") {
var timerId = timer.setTimeout(function () {
defModule.LocationMonitor.stopLocationMonitoring((<any>locListener).id);
location.LocationMonitor.stopLocationMonitoring((<any>locListener).id);
reject(new Error("Timeout while searching for location!"));
}, options.timeout || defaultGetLocationTimeout);
}
});
}
}
8 changes: 4 additions & 4 deletions source/nativescript-geolocation.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import appModule = require("application");
import enums = require("ui/enums");
import timer = require("timer");
import trace = require("trace");
import defModule = require("nativescript-geolocation");
import common = require("./nativescript-geolocation-common");
import {LocationMonitor as LocationMonitorDef} from "./location-monitor";
global.moduleMerge(common, exports);

var locationListeners = {};
Expand Down Expand Up @@ -46,7 +46,7 @@ function createLocationListener() {
}

function locationFromAndroidLocation(androidLocation) {
var location = new defModule.Location();
var location = new common.Location();
location.latitude = androidLocation.getLatitude();
location.longitude = androidLocation.getLongitude();
location.altitude = androidLocation.getAltitude();
Expand Down Expand Up @@ -83,7 +83,7 @@ function androidLocationFromLocation(location) {
return androidLocation;
}

export class LocationMonitor implements defModule.LocationMonitor {
export class LocationMonitor implements LocationMonitorDef {
static getLastKnownLocation() {
var criteria = new android.location.Criteria();
criteria.setAccuracy(android.location.Criteria.ACCURACY_COARSE);
Expand Down Expand Up @@ -193,4 +193,4 @@ export function watchLocation(successCallback, errorCallback, options) {

export function clearWatch(locListenerId) {
LocationMonitor.stopLocationMonitoring(locListenerId);
}
}
6 changes: 3 additions & 3 deletions source/nativescript-geolocation.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ declare module "nativescript-geolocation" {
/**
* The android-specific [location](http://developer.android.com/reference/android/location/Location.html) object.
*/
android: android.location.Location;
android: any;

/**
* The ios-specific [CLLocation](https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocation_Class/) object.
*/
ios: CLLocation;
ios: any;
}

/**
Expand Down Expand Up @@ -98,4 +98,4 @@ declare module "nativescript-geolocation" {
static createListenerWithCallbackAndOptions(successCallback: (location: Location) => void, options: Options): any;
static stopLocationMonitoring(locListenerId: Number): void;
}
}
}
15 changes: 8 additions & 7 deletions source/nativescript-geolocation.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import enums = require("ui/enums");
import timer = require("timer");
import trace = require("trace");
import platformModule = require("platform");
import defModule = require("nativescript-geolocation");
import {Location as LocationDef} from "./location";
import {LocationMonitor as LocationMonitorDef, Options} from "./location-monitor";
import common = require("./nativescript-geolocation-common");
global.moduleMerge(common, exports);

Expand All @@ -23,11 +24,11 @@ class LocationListenerImpl extends NSObject implements CLLocationManagerDelegate
}

private id: number;
private _onLocation: (location: defModule.Location) => any;
private _onLocation: (location: LocationDef) => any;
private _onError: (error: Error) => any
private _options: defModule.Options;
private _options: Options;

public initWithLocationErrorOptions(location: (location: defModule.Location) => any, error?: (error: Error) => any, options?: defModule.Options): LocationListenerImpl {
public initWithLocationErrorOptions(location: (location: LocationDef) => any, error?: (error: Error) => any, options?: Options): LocationListenerImpl {
this._onLocation = location;

if (error) {
Expand Down Expand Up @@ -58,7 +59,7 @@ class LocationListenerImpl extends NSObject implements CLLocationManagerDelegate
}

function locationFromCLLocation(clLocation) {
var location = new defModule.Location();
var location = new common.Location();
location.latitude = clLocation.coordinate.latitude;
location.longitude = clLocation.coordinate.longitude;
location.altitude = clLocation.altitude;
Expand All @@ -83,7 +84,7 @@ function clLocationFromLocation(location) {
return iosLocation;
}

export class LocationMonitor implements defModule.LocationMonitor {
export class LocationMonitor implements LocationMonitorDef {
static getLastKnownLocation() {
var iosLocation;
for(var locManagerId in locationManagers) {
Expand Down Expand Up @@ -193,4 +194,4 @@ export function watchLocation(successCallback, errorCallback, options) {

export function clearWatch(watchId) {
LocationMonitor.stopLocationMonitoring(watchId);
}
}
2 changes: 1 addition & 1 deletion source/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nativescript-geolocation",
"version": "0.0.7",
"version": "0.0.8",
"description": "Provides API for getting and monitoring location for NativeScript app.",
"main": "nativescript-geolocation.js",
"nativescript": {
Expand Down