-
Notifications
You must be signed in to change notification settings - Fork 24.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose heading and missing geolocation properties #13531
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ const LocationEventEmitter = new NativeEventEmitter(RCTLocationObserver); | |
|
||
var subscriptions = []; | ||
var updatesEnabled = false; | ||
var headingUpdatesEnabled = false; | ||
|
||
type GeoOptions = { | ||
timeout: number, | ||
|
@@ -141,6 +142,37 @@ var Geolocation = { | |
} | ||
subscriptions = []; | ||
} | ||
}, | ||
|
||
/* | ||
* Invokes the update callback whenever the location heading changes. | ||
*/ | ||
startUpdatingHeading: function(update: Function) { | ||
if (headingUpdatesEnabled) { | ||
warning('Called startUpdatingHeading while currently updating heading.'); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no-trailing-spaces: Trailing spaces not allowed. |
||
|
||
RCTLocationObserver.startUpdatingHeading(); | ||
headingUpdatesEnabled = true; | ||
|
||
LocationEventEmitter.addListener( | ||
'headingDidChange', | ||
update | ||
); | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no-trailing-spaces: Trailing spaces not allowed. |
||
|
||
/* | ||
* Stops observing a location heading. | ||
*/ | ||
stopUpdatingHeading: function() { | ||
if (!headingUpdatesEnabled) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. semi: Missing semicolon. |
||
warning('Called stopUpdatingHeading without currently updating heading.'); | ||
} | ||
|
||
RCTLocationObserver.stopUpdatingHeading(); | ||
headingUpdatesEnabled = false; | ||
|
||
LocationEventEmitter.remove('headingDidChange'); | ||
} | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
#import <CoreLocation/CLError.h> | ||
#import <CoreLocation/CLLocationManager.h> | ||
#import <CoreLocation/CLLocationManagerDelegate.h> | ||
#import <CoreLocation/CLHeading.h> | ||
|
||
#import <React/RCTAssert.h> | ||
#import <React/RCTBridge.h> | ||
|
@@ -108,6 +109,7 @@ @implementation RCTLocationObserver | |
NSDictionary<NSString *, id> *_lastLocationEvent; | ||
NSMutableArray<RCTLocationRequest *> *_pendingRequests; | ||
BOOL _observingLocation; | ||
BOOL _observingHeading; | ||
RCTLocationOptions _observerOptions; | ||
} | ||
|
||
|
@@ -128,7 +130,7 @@ - (dispatch_queue_t)methodQueue | |
|
||
- (NSArray<NSString *> *)supportedEvents | ||
{ | ||
return @[@"geolocationDidChange", @"geolocationError"]; | ||
return @[@"geolocationDidChange", @"geolocationError", @"headingDidChange"]; | ||
} | ||
|
||
#pragma mark - Private API | ||
|
@@ -267,6 +269,27 @@ - (void)timeout:(NSTimer *)timer | |
[self beginLocationUpdatesWithDesiredAccuracy:accuracy distanceFilter:options.distanceFilter]; | ||
} | ||
|
||
RCT_EXPORT_METHOD(startUpdatingHeading) | ||
{ | ||
if (!_locationManager) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would create a dedicated method for it like |
||
_locationManager = [CLLocationManager new]; | ||
_locationManager.delegate = self; | ||
} | ||
|
||
[_locationManager startUpdatingHeading]; | ||
_observingHeading = YES; | ||
} | ||
|
||
RCT_EXPORT_METHOD(stopUpdatingHeading) | ||
{ | ||
if (!_locationManager || !_observingHeading) { | ||
RCTLogError(@"%@.stopUpdatingHeading called without starting to update.", [self class]); | ||
} | ||
|
||
[_locationManager stopUpdatingHeading]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we deallocate |
||
_observingHeading = NO; | ||
} | ||
|
||
#pragma mark - CLLocationManagerDelegate | ||
|
||
- (void)locationManager:(CLLocationManager *)manager | ||
|
@@ -283,6 +306,8 @@ - (void)locationManager:(CLLocationManager *)manager | |
@"altitudeAccuracy": @(location.verticalAccuracy), | ||
@"heading": @(location.course), | ||
@"speed": @(location.speed), | ||
@"course": @(location.course), | ||
@"floorLevel": @(location.floor.level) | ||
}, | ||
@"timestamp": @([location.timestamp timeIntervalSince1970] * 1000) // in ms | ||
}; | ||
|
@@ -347,6 +372,25 @@ - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError * | |
} | ||
} | ||
|
||
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading | ||
{ | ||
// Create event | ||
NSDictionary *headingEvent = @{ | ||
@"heading": @{ | ||
@"magneticHeading": @(newHeading.magneticHeading), | ||
@"trueHeading": @(newHeading.trueHeading), | ||
@"headingAccuracy": @(newHeading.headingAccuracy), | ||
@"x": @(newHeading.x), | ||
@"y": @(newHeading.y) | ||
} | ||
}; | ||
|
||
// Send event | ||
if (_observingHeading) { | ||
[self sendEventWithName:@"headingDidChange" body:headingEvent]; | ||
} | ||
} | ||
|
||
static void checkLocationConfig() | ||
{ | ||
#if RCT_DEV | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no-trailing-spaces: Trailing spaces not allowed.