Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Pause map's location manager on sleep #3641

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
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
67 changes: 41 additions & 26 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,8 @@ - (void)sleepGL:(__unused NSNotification *)notification
{
self.dormant = YES;

[self validateLocationServices];

[MGLMapboxEvents flush];

_displayLink.paused = YES;
Expand Down Expand Up @@ -1000,6 +1002,8 @@ - (void)wakeGL:(__unused NSNotification *)notification
_mbglMap->resume();

_displayLink.paused = NO;

[self validateLocationServices];
}
}

Expand Down Expand Up @@ -2997,45 +3001,31 @@ - (void)annotationImageNeedsRedisplay:(MGLAnnotationImage *)annotationImage

#pragma mark - User Location -

- (void)setShowsUserLocation:(BOOL)showsUserLocation
- (void)validateLocationServices
{
if (showsUserLocation == _showsUserLocation || _isTargetingInterfaceBuilder) return;

_showsUserLocation = showsUserLocation;
BOOL shouldEnableLocationServices = !self.locationManager && self.showsUserLocation;

if (showsUserLocation)
if (shouldEnableLocationServices)
{
if ([self.delegate respondsToSelector:@selector(mapViewWillStartLocatingUser:)])
{
[self.delegate mapViewWillStartLocatingUser:self];
}

self.userLocationAnnotationView = [[MGLUserLocationAnnotationView alloc] initInMapView:self];
self.userLocationAnnotationView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);

self.locationManager = [CLLocationManager new];
self.locationManager = [[CLLocationManager alloc] init];

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
// enable iOS 8+ location authorization API
//
if ([CLLocationManager instancesRespondToSelector:@selector(requestWhenInUseAuthorization)])
if ([CLLocationManager instancesRespondToSelector:@selector(requestWhenInUseAuthorization)] && [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined)
{
BOOL hasLocationDescription = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"] ||
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"];
BOOL hasLocationDescription = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"] || [[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"];
if (!hasLocationDescription)
{
[NSException raise:@"Missing Location Services usage description" format:
@"In iOS 8 and above, this app must have a value for NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription in its Info.plist."];
@"In iOS 8 and above, this app must have a value for NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription in its Info.plist."];
}
// request location permissions, if both keys exist ask for less permissive
if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"])

if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"])
{
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization];
}
else if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"])
else if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"])
{
[self.locationManager requestAlwaysAuthorization];
[self.locationManager requestWhenInUseAuthorization];
}
}
#endif
Expand All @@ -3050,6 +3040,31 @@ - (void)setShowsUserLocation:(BOOL)showsUserLocation
[self.locationManager stopUpdatingHeading];
self.locationManager.delegate = nil;
self.locationManager = nil;
}
}

- (void)setShowsUserLocation:(BOOL)showsUserLocation
{
if (showsUserLocation == _showsUserLocation || _isTargetingInterfaceBuilder) return;

_showsUserLocation = showsUserLocation;

if (showsUserLocation)
{
if ([self.delegate respondsToSelector:@selector(mapViewWillStartLocatingUser:)])
{
[self.delegate mapViewWillStartLocatingUser:self];
}

self.userLocationAnnotationView = [[MGLUserLocationAnnotationView alloc] initInMapView:self];
self.userLocationAnnotationView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);

[self validateLocationServices];
}
else
{
[self validateLocationServices];

if ([self.delegate respondsToSelector:@selector(mapViewDidStopLocatingUser:)])
{
Expand Down