-
Notifications
You must be signed in to change notification settings - Fork 23
/
RNLocation.m
171 lines (131 loc) · 4.98 KB
/
RNLocation.m
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
#import <CoreLocation/CoreLocation.h>
#import <React/RCTBridge.h>
#import <React/RCTConvert.h>
#import <React/RCTEventDispatcher.h>
#import "RNLocation.h"
@interface RNLocation() <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
@end
@implementation RNLocation
RCT_EXPORT_MODULE()
@synthesize bridge = _bridge;
#pragma mark Initialization
- (instancetype)init
{
if (self = [super init]) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.pausesLocationUpdatesAutomatically = NO;
}
return self;
}
#pragma mark
RCT_EXPORT_METHOD(requestAlwaysAuthorization)
{
NSLog(@"react-native-location: requestAlwaysAuthorization");
[self.locationManager requestAlwaysAuthorization];
}
RCT_EXPORT_METHOD(requestWhenInUseAuthorization)
{
NSLog(@"react-native-location: requestWhenInUseAuthorization");
[self.locationManager requestWhenInUseAuthorization];
}
RCT_EXPORT_METHOD(getAuthorizationStatus:(RCTResponseSenderBlock)callback)
{
callback(@[[self nameForAuthorizationStatus:[CLLocationManager authorizationStatus]]]);
}
RCT_EXPORT_METHOD(setDesiredAccuracy:(double) accuracy)
{
self.locationManager.desiredAccuracy = accuracy;
}
RCT_EXPORT_METHOD(setDistanceFilter:(double) distance)
{
self.locationManager.distanceFilter = distance;
}
RCT_EXPORT_METHOD(setAllowsBackgroundLocationUpdates:(BOOL) enabled)
{
self.locationManager.allowsBackgroundLocationUpdates = enabled;
}
RCT_EXPORT_METHOD(startMonitoringSignificantLocationChanges)
{
NSLog(@"react-native-location: startMonitoringSignificantLocationChanges");
[self.locationManager startMonitoringSignificantLocationChanges];
}
RCT_EXPORT_METHOD(startUpdatingLocation)
{
[self.locationManager startUpdatingLocation];
}
RCT_EXPORT_METHOD(startUpdatingHeading)
{
[self.locationManager startUpdatingHeading];
}
RCT_EXPORT_METHOD(stopMonitoringSignificantLocationChanges)
{
[self.locationManager stopMonitoringSignificantLocationChanges];
}
RCT_EXPORT_METHOD(stopUpdatingLocation)
{
[self.locationManager stopUpdatingLocation];
}
RCT_EXPORT_METHOD(stopUpdatingHeading)
{
[self.locationManager stopUpdatingHeading];
}
-(NSString *)nameForAuthorizationStatus:(CLAuthorizationStatus)authorizationStatus
{
switch (authorizationStatus) {
case kCLAuthorizationStatusAuthorizedAlways:
NSLog(@"Authorization Status: authorizedAlways");
return @"authorizedAlways";
case kCLAuthorizationStatusAuthorizedWhenInUse:
NSLog(@"Authorization Status: authorizedWhenInUse");
return @"authorizedWhenInUse";
case kCLAuthorizationStatusDenied:
NSLog(@"Authorization Status: denied");
return @"denied";
case kCLAuthorizationStatusNotDetermined:
NSLog(@"Authorization Status: notDetermined");
return @"notDetermined";
case kCLAuthorizationStatusRestricted:
NSLog(@"Authorization Status: restricted");
return @"restricted";
}
}
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
NSString *statusName = [self nameForAuthorizationStatus:status];
[self.bridge.eventDispatcher sendDeviceEventWithName:@"authorizationStatusDidChange" body:statusName];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"Location manager failed: %@", error);
}
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
if (newHeading.headingAccuracy < 0)
return;
// Use the true heading if it is valid.
CLLocationDirection heading = ((newHeading.trueHeading > 0) ?
newHeading.trueHeading : newHeading.magneticHeading);
NSDictionary *headingEvent = @{
@"heading": @(heading)
};
NSLog(@"heading: %f", heading);
[self.bridge.eventDispatcher sendDeviceEventWithName:@"headingUpdated" body:headingEvent];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *location = [locations lastObject];
NSDictionary *locationEvent = @{
@"latitude": @(location.coordinate.latitude),
@"longitude": @(location.coordinate.longitude),
@"altitude": @(location.altitude),
@"accuracy": @(location.horizontalAccuracy),
@"altitudeAccuracy": @(location.verticalAccuracy),
@"course": @(location.course),
@"speed": @(location.speed),
@"timestamp": @([location.timestamp timeIntervalSince1970] * 1000) // in ms
};
NSLog(@"%@: lat: %f, long: %f, altitude: %f", location.timestamp, location.coordinate.latitude, location.coordinate.longitude, location.altitude);
[self.bridge.eventDispatcher sendDeviceEventWithName:@"locationUpdated" body:locationEvent];
}
@end