forked from captainhurst/react-native-location
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RNLocation.m
152 lines (115 loc) · 4.13 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
#import <CoreLocation/CoreLocation.h>
// #import "RCTBridge.h"
#import "RCTConvert.h"
#import "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;
if ([self.locationManager respondsToSelector:@selector(allowsBackgroundLocationUpdates)]) {
self.locationManager.allowsBackgroundLocationUpdates = YES;
}
}
return self;
}
#pragma mark
RCT_EXPORT_METHOD(requestAlwaysAuthorization)
{
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
}
}
RCT_EXPORT_METHOD(requestWhenInUseAuthorization)
{
if ([self.locationManager respondsToSelector:@selector(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(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:
return @"authorizedAlways";
case kCLAuthorizationStatusAuthorizedWhenInUse:
return @"authorizedWhenInUse";
case kCLAuthorizationStatusDenied:
return @"denied";
case kCLAuthorizationStatusNotDetermined:
return @"notDetermined";
case kCLAuthorizationStatusRestricted:
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 didUpdateLocations:(NSArray *)locations {
CLLocation *location = [locations lastObject];
NSDictionary *locationEvent = @{
@"coords": @{
@"latitude": @(location.coordinate.latitude),
@"longitude": @(location.coordinate.longitude),
@"altitude": @(location.altitude),
@"accuracy": @(location.horizontalAccuracy),
@"altitudeAccuracy": @(location.verticalAccuracy),
@"heading": @(location.course),
@"speed": @(location.speed),
},
@"timestamp": @(CFAbsoluteTimeGetCurrent() * 1000.0) // in ms
};
[self.bridge.eventDispatcher sendDeviceEventWithName:@"locationUpdated" body:locationEvent];
}
@end