forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocation.ios.ts
More file actions
180 lines (152 loc) · 7.2 KB
/
location.ios.ts
File metadata and controls
180 lines (152 loc) · 7.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import enums = require("ui/enums");
import locationModule = require("location");
import common = require("location/location-common");
import merger = require("utils/module-merge");
declare var exports;
merger.merge(common, exports);
class LocationListenerImpl extends NSObject implements CLLocationManagerDelegate {
public static ObjCProtocols = [CLLocationManagerDelegate];
static new(): LocationListenerImpl {
return <LocationListenerImpl>super.new();
}
private _onLocation: (location: locationModule.Location) => any;
private _onError: (error: Error) => any
private _options: locationModule.Options;
private _maximumAge: number;
public initWithLocationErrorOptions(location: (location: locationModule.Location) => any, error?: (error: Error) => any, options?: locationModule.Options): LocationListenerImpl {
this._onLocation = location;
if (error) {
this._onError = error;
}
if (options) {
this._options = options;
}
this._maximumAge = (this._options && ("number" === typeof this._options.maximumAge)) ? this._options.maximumAge : undefined;
return this;
}
public locationManagerDidUpdateLocations(manager, locations): void {
for (var i = 0; i < locations.count; i++) {
var location = LocationManager._locationFromCLLocation(locations.objectAtIndex(i));
if (this._maximumAge) {
if (location.timestamp.valueOf() + this._maximumAge > new Date().valueOf()) {
this._onLocation(location);
}
}
else {
this._onLocation(location);
}
}
}
public locationManagerDidFailWithError(manager, error): void {
if (this._onError) {
this._onError(new Error(error.localizedDescription));
}
}
}
export class LocationManager implements locationModule.LocationManager {
get android(): locationModule.AndroidLocationManager {
return undefined;
}
get ios(): locationModule.iOSLocationManager {
return this.iosLocationManager;
}
public isStarted: boolean;
// in meters
// we might need some predefined values here like 'any' and 'high'
public desiredAccuracy: number;
// The minimum distance (measured in meters) a device must move horizontally before an update event is generated.
public updateDistance: number;
private iosLocationManager: locationModule.iOSLocationManager;
private listener: any;
public static _locationFromCLLocation(clLocation: CLLocation): locationModule.Location {
var location = new locationModule.Location();
location.latitude = clLocation.coordinate.latitude;
location.longitude = clLocation.coordinate.longitude;
location.altitude = clLocation.altitude;
location.horizontalAccuracy = clLocation.horizontalAccuracy;
location.verticalAccuracy = clLocation.verticalAccuracy;
location.speed = clLocation.speed;
location.direction = clLocation.course;
var timeIntervalSince1970 = NSDate.dateWithTimeIntervalSinceDate(0, clLocation.timestamp).timeIntervalSince1970;
location.timestamp = new Date(timeIntervalSince1970 * 1000);
location.ios = clLocation;
return location;
}
private static iosLocationFromLocation(location: locationModule.Location): CLLocation {
var hAccuracy = location.horizontalAccuracy ? location.horizontalAccuracy : -1;
var vAccuracy = location.verticalAccuracy ? location.verticalAccuracy : -1;
var speed = location.speed ? location.speed : -1;
var course = location.direction ? location.direction : -1;
var altitude = location.altitude ? location.altitude : -1;
var timestamp = location.timestamp ? NSDate.dateWithTimeIntervalSince1970(location.timestamp.getTime() / 1000) : null;
var iosLocation = CLLocation.alloc().initWithCoordinateAltitudeHorizontalAccuracyVerticalAccuracyCourseSpeedTimestamp(CLLocationCoordinate2DMake(location.latitude, location.longitude), altitude, hAccuracy, vAccuracy, course, speed, timestamp);
return iosLocation;
}
public static isEnabled(): boolean {
if (CLLocationManager.locationServicesEnabled()) {
// CLAuthorizationStatus.kCLAuthorizationStatusAuthorizedWhenInUse and CLAuthorizationStatus.kCLAuthorizationStatusAuthorizedAlways are options that are available in iOS 8.0+
// while CLAuthorizationStatus.kCLAuthorizationStatusAuthorized is here to support iOS 8.0-.
return (CLLocationManager.authorizationStatus() === CLAuthorizationStatus.kCLAuthorizationStatusAuthorizedWhenInUse
|| CLLocationManager.authorizationStatus() === CLAuthorizationStatus.kCLAuthorizationStatusAuthorizedAlways
|| CLLocationManager.authorizationStatus() === CLAuthorizationStatus.kCLAuthorizationStatusAuthorized);
}
return false;
}
public static distance(loc1: locationModule.Location, loc2: locationModule.Location): number {
if (!loc1.ios) {
loc1.ios = LocationManager.iosLocationFromLocation(loc1);
}
if (!loc2.ios) {
loc2.ios = LocationManager.iosLocationFromLocation(loc2);
}
return loc1.ios.distanceFromLocation(loc2.ios);
}
constructor() {
//super();
this.desiredAccuracy = enums.Accuracy.any;
this.updateDistance = kCLDistanceFilterNone;
var iosLocManager = new CLLocationManager();
this.iosLocationManager = new iOSLocationManager(iosLocManager);
}
public startLocationMonitoring(onLocation: (location: locationModule.Location) => any, onError?: (error: Error) => any, options?: locationModule.Options) {
if (!this.listener) {
if (options) {
if (options.desiredAccuracy) {
this.desiredAccuracy = options.desiredAccuracy;
}
if (options.updateDistance) {
this.updateDistance = options.updateDistance;
}
}
this.listener = LocationListenerImpl.new().initWithLocationErrorOptions(onLocation, onError, options);
this.iosLocationManager.manager.delegate = this.listener;
this.iosLocationManager.manager.desiredAccuracy = this.desiredAccuracy;
this.iosLocationManager.manager.distanceFilter = this.updateDistance;
this.iosLocationManager.manager.startUpdatingLocation();
this.isStarted = true;
}
}
public stopLocationMonitoring() {
this.iosLocationManager.manager.stopUpdatingLocation();
this.iosLocationManager.manager.delegate = null;
this.listener = null;
this.isStarted = false;
}
get lastKnownLocation(): locationModule.Location {
var clLocation = this.iosLocationManager.manager.location;
if (clLocation) {
return LocationManager._locationFromCLLocation(clLocation);
}
return null;
}
}
/* tslint:disable */
export class iOSLocationManager implements locationModule.iOSLocationManager {
private _manager: CLLocationManager;
public get manager(): CLLocationManager {
return this._manager;
}
constructor(manager: CLLocationManager) {
this._manager = manager;
}
}