菜鸟教程小白 发表于 2022-12-12 18:26:35

ios - 区域监控(地理围栏)会消耗电池电量(iOS)


                                            <p><p>我已经在我的应用中实现了 <code>CLLocationManager</code> 的区域监控功能,它可以工作但它会耗尽我的电池:</p>

<p>-</p>

<p> <a href="/image/qGxQ4.png" rel="noreferrer noopener nofollow"><img src="/image/qGxQ4.png" alt="Image"/></a> </p>

<p>-</p>

<p>应该这样吗?</p>

<p>我的代码:</p>

<p><strong>monitorLocationViewController.m</strong>(请滚动查看完整代码):</p>

<pre><code>-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray&lt;CLLocation *&gt; *)locations
{
    //If &#34;allStores&#34;(NSMutableArray) isn&#39;t nil - calling &#34;locationChangeHandler&#34; to update monitoring
    if (self.allStores!=nil) {
      ;
    }

    CLLocation *currentLocation=(CLLocation*);
    NSSet *monitoredRegionsSet=self.locationManager.monitoredRegions;
    [monitoredRegionsSet enumerateObjectsUsingBlock:^(CLCircularRegion *region, BOOL *stop) {
      if () {
            ;
            ;
      }
    }];
}

-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
   Store *store=;
    if (store.alreadySendNotification==NO) {
      UILocalNotification *notification=[ init];
      notification.alertTitle=@&#34;Arounder&#34;;
      notification.alertBody=[ address];
      [ scheduleLocalNotification:notification];

      store.alreadySendNotification=YES;
    }
}

    //For updating monitoring
-(void)locationChangeHandler
{
//If &#34;allStores&#34;(NSMutableArray) isn&#39;t nil
    if (self.allStores!=nil) {
      //Finding the 20 closest stores to he user&#39;s location and adding it to &#34;twentyClosestStores&#34;(NSMutableArray)
      ;
      //Stop monitoring &#34;previousTwentyStores&#34;(NSMutableArray) (20 closest stores before user&#39;s locationupdated)
      ;
      //Start monitoring &#34;twentyClosestStores&#34;(NSMutableArray)
      ;
    }
}

//Start monitoring &#34;twentyClosestStores&#34;(NSMutableArray)
-(void)startMonitoringClosestStores
{
    //If monitoring isn&#39;t availible for &#34;CLCircularRegion&#34;
    if (!]) {
      NSLog(@&#34;Monitoring is not available for CLCircularRegion class&#34;);
      return;
    }

    //Run on all &#34;twentyClosestStores&#34;(NSMutableArray)&#39;s objects
    for (Store *currentStore in self.twentyClosestStores) {
      //Start monitoring &#34;region&#34;(CLCircularRegion)
      ;
    }
}

//Stop monitoring &#34;previousTwentyStores&#34;(NSMutableArray) (20 closest stores before user&#39;s locationupdated)
-(void)stopMonitoringStores
{
    //Run on all &#34;monitoredRegions&#34;(NSSet) of &#34;locationManager&#34;(CLLocationManager) objects
    for (CLCircularRegion *currentRegion in self.locationManager.monitoredRegions) {
      //Stop monitoring &#34;region&#34;(CLCircularRegion)
      ;
    }
}

//Finding a store for region
-(Store*)storeForRegion:(CLCircularRegion*)region
{
    //Run on all &#34;allStores&#34;(NSMutableArray)&#39;s objects
    for (Store *currentStore in self.allStores) {
      //If &#34;currentStore&#34;(Store)&#39;s &#34;circularRegion&#34;&#39;s identifier is equal to &#34;region&#34;(CLCircularRegion)&#39;s identifier
      if () {
            //Returning &#34;currentStore&#34;(Store)
            return currentStore;
      }
    }
    //Store not found - returning nil
    NSLog(@&#34;No store found for this region: %f,%f&#34;,region.center.latitude,region.center.longitude);
    return nil;
}
</code></pre>

<p><strong>AppDelegate.m</strong>:</p>

<pre><code>-(BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.monitorLocationVC=[ init];
    self.monitorLocationVC.locationManager=self.locationManager;

    ;
    ;

    return YES;
}

-(void)configureLocationManager
{
    //Initializing locationManager
    self.locationManager=[ init];
    //setting &#34;locationManager&#34;&#39;s(CLLocationManager) delegate to &#34;self&#34;
    self.locationManager.delegate=self.monitorLocationVC;
    //Setting &#34;locationManager&#34;&#39;s(CLLocationManager)&#39;s distance filter to none
    self.locationManager.distanceFilter=kCLDistanceFilterNone;
    //Setting &#34;locationManager&#34;&#39;s(CLLocationManager)&#39;s activityType to navigation
    self.locationManager.activityType=CLActivityTypeAutomotiveNavigation;
    //setting &#34;locationManager&#34;&#39;s(CLLocationManager) desiredAccuracy to &#34;best&#34;
    self.locationManager.desiredAccuracy=kCLLocationAccuracyBestForNavigation;

    self.locationManager.pausesLocationUpdatesAutomatically=NO;

    //If OS version is 9 or above - setting &#34;allowsBackgroundLocationUpdates&#34; to YES
    if ([[ systemVersion] floatValue] &gt;= 9) {
      self.locationManager.allowsBackgroundLocationUpdates = YES;
    }
}
</code></pre>

<p>谢谢!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您只想监控区域,而不是在后台不断更新它们的位置。</p>

<p>试试这个:</p>

<pre><code>self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
</code></pre>

<p>你真的需要将 distanceFilter 设置为 <code>kCLDistanceFilterNone</code> 吗?这将导致使用更多的电池电量。您可能想尝试将其设置为 10、20、50 甚至 100 米左右。</p>

<p>另外,为了不经常更新位置,而不是:</p>

<pre><code>;
</code></pre>

<p>尝试使用:</p>

<pre><code>;
</code></pre>

<p>所有这些都应该有助于减少电池使用量。当您将精度和距离过滤器设置为尽可能高的设置时,电池将会耗尽。</p>

<p><strong>编辑:</strong>
由于您的应用程序的目的,无论您做什么,都会消耗大量电池。我之前针对类似问题所做的一个解决方案是使用 NSTimer 创建算法或公式,该 NSTimer 每 x 分钟触发一次以更新用户的位置。 (但仅在区域移动 x 米时才更新区域)。</p>

<ul>
<li>在 NSTimer 触发之间停止位置更新,这样您就不会不断更新位置。</li>
<li>当计时器触发时,恢复位置更新,抓取大约 10 个位置(这样您就可以获得准确的位置),然后关闭位置更新,直到下次触发计时器时</li>
</ul></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 区域监控(地理围栏)会消耗电池电量(iOS),我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/35208989/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/35208989/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 区域监控(地理围栏)会消耗电池电量(iOS)