我一直在尝试实现一个位置视图控制器,该控制器不断将用户位置更新到服务器。当我调试应用程序并在使用时,我注意到,我提供的源代码比平时更快地耗尽了手机电池。我是否必须处理这个问题,或者有什么方法可以在不降低位置要求的情况下改善以下代码中的电源使用情况?
class LocationViewController: UIViewController {
// MARK: - Outlets
@IBOutlet var mapView: MKMapView!
fileprivate var locations = [MKPointAnnotation]()
var updatesEnabled: Bool = false;
var defaultCentre: NotificationCenter = NotificationCenter.default
//- NSUserDefaults - LocationServicesControl_KEY to be set to TRUE when user has enabled location services.
let UDefaults: UserDefaults = UserDefaults.standard;
let LocationServicesControl_KEY: String = "LocationServices"
public var lastPosition: Date?;
public lazy var locationManager: CLLocationManager = {
let manager = CLLocationManager()
manager.distanceFilter = 20;
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.delegate = self
manager.requestAlwaysAuthorization()
return manager
}()
public var routeLine: MKPolyline = MKPolyline() //your line
public var routeLineView: MKPolylineView = MKPolylineView(); //overlay view
// MARK: - Actions
@available(iOS 9.0, *)
@IBAction func enabledChanged(_ sender: UISwitch) {
if sender.isOn {
self.UDefaults.set(true, forKey: self.LocationServicesControl_KEY)
locationManager.startUpdatingLocation()
locationManager.allowsBackgroundLocationUpdates = true
self.updatesEnabled = true;
} else {
self.UDefaults.set(false, forKey: self.LocationServicesControl_KEY)
locationManager.stopUpdatingLocation()
self.updatesEnabled = false;
self.timer.invalidate()
locationManager.allowsBackgroundLocationUpdates = false
}
}
@IBAction func accuracyChanged(_ sender: UISegmentedControl) {
let accuracyValues = [
kCLLocationAccuracyBestForNavigation,
kCLLocationAccuracyBest,
kCLLocationAccuracyNearestTenMeters,
kCLLocationAccuracyHundredMeters,
kCLLocationAccuracyKilometer,
kCLLocationAccuracyThreeKilometers]
locationManager.desiredAccuracy = accuracyValues[sender.selectedSegmentIndex];
}
// MARK: - Override UIViewController
override func viewDidLoad() {
mapView.delegate = self;
}
}
// MARK: - MKMapViewDelegate
extension LocationViewController: MKMapViewDelegate {
func addRoute() {
mapView.remove(self.routeLine);
var pointsToUse: [CLLocationCoordinate2D] = [];
for i in locations {
pointsToUse += [i.coordinate];
}
self.routeLine = MKPolyline(coordinates: &pointsToUse, count: pointsToUse.count)
mapView.add(self.routeLine)
}
}
// MARK: - CLLocationManagerDelegate
extension LocationViewController: CLLocationManagerDelegate {
func isUpdateValid (newDate: Date) -> Bool{
var interval = TimeInterval()
if(lastPosition==nil){lastPosition=newDate}
interval = newDate.timeIntervalSince(lastPosition!)
if ((interval==0)||(interval>=self.UpdatesInterval)){
return true
} else {
return false
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let mostRecentLocation = locations.last else {
return
}
if(isUpdateValid(newDate: mostRecentLocation.timestamp)){
let position = MKPointAnnotation();
position.coordinate=mostRecentLocation.coordinate;
self.locations.append(position);
self.addRoute();
self.locationAPI.postLocation(location: mostRecentLocation)
lastPosition=mostRecentLocation.timestamp
}
}
}
您需要以某种方式限制 GPS "点亮"的时间。如果你把它设置为持续提供高精度的GPS读数,它将保持GPS的通电,你将使用大量的电力。对此无能为力。
可能性:
您可以每 10 分钟唤醒一次 GPS,运行它直到获得良好的读数,然后将其关闭。
当你的应用首次启动时,你会得到很好的阅读,然后订阅重要的位置更改,当你获得更新时,开始位置更新,获取新的修复,然后再次关闭位置更新。
我不确定您将如何执行第一个选项,因为Apple不会让您的应用程序在后台无限期运行。你需要你的应用一直处于唤醒状态并在前台运行,这也比允许它进入睡眠状态要快得多。