{"action":"created","issue":{"url":"https://api.github.com/repos/mapbox/react-native-mapbox-gl/issues/77","labels_url":"https://api.github.com/repos/mapbox/react-native-mapbox-gl/issues/77/labels{/name}","comments_url":"https://api.github.com/repos/mapbox/react-native-mapbox-gl/issues/77/comments","events_url":"https://api.github.com/repos/mapbox/react-native-mapbox-gl/issues/77/events","html_url":"https://github.com/mapbox/react-native-mapbox-gl/issues/77","id":100161772,"number":77,"title":"Updating user-location in background","user":{"login":"christocracy","id":14999,"avatar_url":"https://avatars.githubusercontent.com/u/14999?v=3","gravatar_id":"","url":"https://api.github.com/users/christocracy","html_url":"https://github.com/christocracy","followers_url":"https://api.github.com/users/christocracy/followers","following_url":"https://api.github.com/users/christocracy/following{/other_user}","gists_url":"https://api.github.com/users/christocracy/gists{/gist_id}","starred_url":"https://api.github.com/users/christocracy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/christocracy/subscriptions","organizations_url":"https://api.github.com/users/christocracy/orgs","repos_url":"https://api.github.com/users/christocracy/repos","events_url":"https://api.github.com/users/christocracy/events{/privacy}","received_events_url":"https://api.github.com/users/christocracy/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-08-10T20:28:28Z","updated_at":"2015-11-13T12:50:34Z","closed_at":"2015-08-15T01:54:53Z","body":"I've created a sophisticated [background-geolocation](https://github.com/christocracy/react-native-background-geolocation) plugin (ported from my popular [Cordova plugin](http://transistorsoft.github.io/cordova-background-geolocation/) 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).\r\n\r\nBecause 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.\r\n\r\nIn spite of what [this link](https://www.mapbox.com/guides/first-steps-gl-ios/#background-location) says.\r\n\r\n>How does background location data affect device battery usage? We determined that background \r\n>location data is not a significant hit to the battery through extensive testing. You can read about how we \r\n>tested the battery on the blog.\r\n\r\nAfter 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.\r\n\r\nThis is what my plugin resolves.\r\n\r\nA simple (very raw) solution in `RCTMapboxGL.m` goes like this:\r\n\r\n```\r\n- (void)createMap\r\n{\r\n    [MGLAccountManager setAccessToken:_accessToken];\r\n    _map = [[MGLMapView alloc] initWithFrame:self.bounds styleURL:_styleURL];\r\n    _map.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;\r\n    _map.delegate = self;\r\n    _map.userTrackingMode = MGLUserTrackingModeFollow;\r\n    [self updateMap];\r\n    [self addSubview:_map];\r\n    [self layoutSubviews];\r\n    \r\n    // Solution:  Listen to pause/resume events so we can start/stop locationManager\r\n    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onSuspend:) name:UIApplicationDidEnterBackgroundNotification object:nil];\r\n    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onResume:) name:UIApplicationWillEnterForegroundNotification object:nil];\r\n}\r\n\r\n- (void) onSuspend:(NSNotification*)notification\r\n{\r\n    // Stop updating location.  \r\n   _map.showsUserLocation = NO;\r\n}\r\n- (void) onResume:(NSNotification*)notification\r\n{\r\n     // Restore updating locations\r\n    _map.showsUserLocation = YES;\r\n}\r\n\r\nThoughts?\r\n"},"comment":{"url":"https://api.github.com/repos/mapbox/react-native-mapbox-gl/issues/comments/156424298","html_url":"https://github.com/mapbox/react-native-mapbox-gl/issues/77#issuecomment-156424298","issue_url":"https://api.github.com/repos/mapbox/react-native-mapbox-gl/issues/77","id":156424298,"user":{"login":"byroncoetsee","id":4600132,"avatar_url":"https://avatars.githubusercontent.com/u/4600132?v=3","gravatar_id":"","url":"https://api.github.com/users/byroncoetsee","html_url":"https://github.com/byroncoetsee","followers_url":"https://api.github.com/users/byroncoetsee/followers","following_url":"https://api.github.com/users/byroncoetsee/following{/other_user}","gists_url":"https://api.github.com/users/byroncoetsee/gists{/gist_id}","starred_url":"https://api.github.com/users/byroncoetsee/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/byroncoetsee/subscriptions","organizations_url":"https://api.github.com/users/byroncoetsee/orgs","repos_url":"https://api.github.com/users/byroncoetsee/repos","events_url":"https://api.github.com/users/byroncoetsee/events{/privacy}","received_events_url":"https://api.github.com/users/byroncoetsee/received_events","type":"User","site_admin":false},"created_at":"2015-11-13T12:50:34Z","updated_at":"2015-11-13T12:50:34Z","body":"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.\r\n\r\nAnyone else had this issue?"}}