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

Updating user-location in background #77

Closed
christocracy opened this issue Aug 10, 2015 · 6 comments
Closed

Updating user-location in background #77

christocracy opened this issue Aug 10, 2015 · 6 comments

Comments

@christocracy
Copy link

I've created a sophisticated background-geolocation plugin (ported from my popular Cordova plugin which intelligently monitors the user's movement-state and only engages locationManager when device is determined to be moving (and stops locationManager when the device has stopped).

Because my plugin sets NSLocationAlwaysUsageDescription, react-native-mapbox-gl will keep locationManager updating the location even when the device goes to the background, which kills the battery in a short time, of course. This is what my plugin resolves.

In spite of what this link says.

How does background location data affect device battery usage? We determined that background
location data is not a significant hit to the battery through extensive testing. You can read about how we
tested the battery on the blog.

After over 2 years experience creating Android/iOS background geolocation code, keeping locationManager always running in background WILL certainly kill the battery in a very short time.

This is what my plugin resolves.

A simple (very raw) solution in RCTMapboxGL.m goes like this:

- (void)createMap
{
    [MGLAccountManager setAccessToken:_accessToken];
    _map = [[MGLMapView alloc] initWithFrame:self.bounds styleURL:_styleURL];
    _map.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    _map.delegate = self;
    _map.userTrackingMode = MGLUserTrackingModeFollow;
    [self updateMap];
    [self addSubview:_map];
    [self layoutSubviews];

    // Solution:  Listen to pause/resume events so we can start/stop locationManager
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onSuspend:) name:UIApplicationDidEnterBackgroundNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onResume:) name:UIApplicationWillEnterForegroundNotification object:nil];
}

- (void) onSuspend:(NSNotification*)notification
{
    // Stop updating location.  
   _map.showsUserLocation = NO;
}
- (void) onResume:(NSNotification*)notification
{
     // Restore updating locations
    _map.showsUserLocation = YES;
}

Thoughts?
@bsudekum
Copy link

I think this looks good. @christocracy want to make this into a PR?

@christocracy
Copy link
Author

I'll clean it up, add required logic and do a PR.

On Monday, August 10, 2015, Bobby Sudekum notifications@github.com wrote:

I think this looks good. @christocracy https://github.com/christocracy
want to make this into a PR?


Reply to this email directly or view it on GitHub
#77 (comment)
.

Snet form Gmail Mobile

@friedbunny
Copy link
Contributor

@christocracy The place to submit this fix would be upstream in mapbox/mapbox-gl-native, sticking it someplace in platform/ios/MGLMapView.mm.

@christocracy
Copy link
Author

Ok, I'll swim upstream then.

@byroncoetsee
Copy link

I realise this might be old but incase anyone might have missed this, I feel I should put it out there... This does work but only if the user agrees to Location Permission request before... When testing this method (Using Swift 1.2, iOS 8, XCode 6, iPhone 6), I get the Permission Request window opening up continuously every second. open close open close etc.

Anyone else had this issue?

@bsudekum
Copy link

@byroncoetsee confirmed, I can take a look at this bug this weekend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants