Skip to content
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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Libraries/Geolocation/Geolocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const LocationEventEmitter = new NativeEventEmitter(RCTLocationObserver);

var subscriptions = [];
var updatesEnabled = false;
var headingUpdatesEnabled = false;

type GeoOptions = {
timeout: number,
Expand Down Expand Up @@ -141,6 +142,37 @@ var Geolocation = {
}
subscriptions = [];
}
},

/*

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.

* Invokes the update callback whenever the location heading changes.
*/
startUpdatingHeading: function(update: Function) {
if (headingUpdatesEnabled) {
warning('Called startUpdatingHeading while currently updating heading.');
}

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.


RCTLocationObserver.startUpdatingHeading();
headingUpdatesEnabled = true;

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.


LocationEventEmitter.addListener(
'headingDidChange',
update
);
},

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.


/*
* Stops observing a location heading.
*/
stopUpdatingHeading: function() {
if (!headingUpdatesEnabled) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

semi: Missing semicolon.

warning('Called stopUpdatingHeading without currently updating heading.')
}

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.


RCTLocationObserver.stopUpdatingHeading();
headingUpdatesEnabled = false;

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.


LocationEventEmitter.remove('headingDidChange');
}
};

Expand Down
46 changes: 45 additions & 1 deletion Libraries/Geolocation/RCTLocationObserver.m
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -108,6 +109,7 @@ @implementation RCTLocationObserver
NSDictionary<NSString *, id> *_lastLocationEvent;
NSMutableArray<RCTLocationRequest *> *_pendingRequests;
BOOL _observingLocation;
BOOL _observingHeading;
RCTLocationOptions _observerOptions;
}

Expand All @@ -128,7 +130,7 @@ - (dispatch_queue_t)methodQueue

- (NSArray<NSString *> *)supportedEvents
{
return @[@"geolocationDidChange", @"geolocationError"];
return @[@"geolocationDidChange", @"geolocationError", @"headingDidChange"];
}

#pragma mark - Private API
Expand Down Expand Up @@ -267,6 +269,27 @@ - (void)timeout:(NSTimer *)timer
[self beginLocationUpdatesWithDesiredAccuracy:accuracy distanceFilter:options.distanceFilter];
}

RCT_EXPORT_METHOD(startUpdatingHeading)
{
if (!_locationManager) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would create a dedicated method for it like initLocationManagerIfNeeded.

_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];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we deallocate _locationManager somewhere?

_observingHeading = NO;
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager
Expand All @@ -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
};
Expand Down Expand Up @@ -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
Expand Down