使用LocationManager时减少电池使用

时间:2018-02-06 23:49:57

标签: ios swift cllocationmanager

我正在制作一个需要每隔10秒检查一次位置的程序

我确实在应用代理中启动了位置管理器并将其保留在那里:

LongListMarshaller

我做了很多研究,但没有找到任何东西。有什么能减少电池使用量吗?

例如,每次收到位置时停止// I need it to be precise locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters locationManager.startUpdatingLocation() ,并在10秒过去并且程序需要位置时再次启动?有什么建议吗?

2 个答案:

答案 0 :(得分:2)

有两个主要因素:

  1. 位置
  2. 精度
  3. 首先,你为什么每隔10秒就需要它?这非常频繁。 每分钟或几分钟怎么样?

    其次,您需要十米精度吗? 降低精度将使操作系统节省电力。

    最节能的方法是监控重要的位置变化,例如here

    检查您的用例。允许操作系统留出更多余地将改善电池寿命。要求最精确的准确度,每隔10秒就会很快耗尽它。请在提问时将您的用例发布。

答案 1 :(得分:0)

1. Can reduce accuracy - More will be accuracy more GPS power consumption is there for finding location even accuracy is nearestThreeKilometer is can give about 100 meter accuracy or so.   
       e.g locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers

1.  Use distance Filter instead time filter - e.g locationManager.distanceFilter = 1000 it will get called whenever location changed by 1000m.
2.  Don’t allow app to discover location is background  &  not enable background mode in capability
    locationManager.allowsBackgroundLocationUpdates = false 

3. If GPS-level accuracy isn’t critical for your app, you don’t need continuous tracking, and region or visit monitoring isn’t more appropriate for your app, you can use the significant-change location service instead of the standard one
4. Use Request Location instead startUpdatingLocation() to get location one time only.e.g   self.locationManager.requestLocation()
5. You can help the determination of when to pause location updates by assigning a value to the activityType property. 
  locationManager.pausesLocationUpdatesAutomatically = true
  // Specify the type of activity your app is currently performing
  locationManager.activityType = CLActivityType.automotiveNavigation

6. Defer Location Updates When Running in the Background Apple Link

7. Finally you can find Apple doc Location Best practices