-
Notifications
You must be signed in to change notification settings - Fork 34
/
ReactNativeHeading.m
70 lines (53 loc) · 1.81 KB
/
ReactNativeHeading.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
//
// ReactNativeHeading.m
// ReactNativeHeading
//
// Created by Yonah Forst on 18/02/16.
// Copyright © 2016 Yonah Forst. All rights reserved.
//
#import "ReactNativeHeading.h"
#import <React/RCTBridge.h>
#import <React/RCTConvert.h>
#import <React/RCTEventDispatcher.h>
#import <CoreLocation/CoreLocation.h>
@interface ReactNativeHeading() <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locManager;
@property (nonatomic) CLLocationDirection currentHeading;
@end
@implementation ReactNativeHeading
RCT_EXPORT_MODULE();
@synthesize bridge = _bridge;
#pragma mark Initialization
- (instancetype)init
{
if (self = [super init]) {
self.locManager = [[CLLocationManager alloc] init];
self.locManager.delegate = self;
}
return self;
}
RCT_REMAP_METHOD(start, start:(int)headingFilter resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
// Start heading updates.
if ([CLLocationManager headingAvailable]) {
if (!headingFilter)
headingFilter = 5;
self.locManager.headingFilter = headingFilter;
[self.locManager startUpdatingHeading];
resolve(@YES);
} else {
resolve(@NO);
}
}
RCT_EXPORT_METHOD(stop) {
[self.locManager stopUpdatingHeading];
}
- (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)};
[self.bridge.eventDispatcher sendDeviceEventWithName:@"headingUpdated" body:headingEvent];
}
@end