【问题标题】:Core Location in SwiftSwift 中的核心位置
【发布时间】:2014-08-27 22:17:46
【问题描述】:

这是示例代码。 NSLocationAlwaysUsageDescription 已安装 Info.plist 中的键。一切正常 - 程序接收坐标......显示它们......

它会继续永久更新它们!在 iOS 模拟器的情况下 - 这并不重要,但在真正的应用程序的情况下,它会很快耗尽电池。如何让应用启动Core Location,接收到位置,关闭Core Location?

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet var locLabel: UILabel
@IBOutlet var latLabel: UILabel
@IBOutlet var lonLabel: UILabel

var locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

@IBAction func locButton(sender: AnyObject) {
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.startUpdatingLocation()
}

func locationManager(manager:CLLocationManager, didUpdateLocations locations:AnyObject[]) {
    println("locations = \(locationManager)")
    var latValue = locationManager.location.coordinate.latitude
    var lonValue = locationManager.location.coordinate.longitude
    latLabel.text = String(latValue)
    lonLabel.text = String(lonValue)
    locLabel.text = "success"
}

}

【问题讨论】:

  • 在didUpdateLocations中,对当前位置的精度/时间戳满意后,可以调用stopUpdatingLocation()。

标签: swift core-location cllocationmanager ios8


【解决方案1】:

如果您不太关心高精度,那么您应该考虑使用startMonitoringSignificantLocationChanges 而不是startUpdatingLocation。它确实会对电池消耗产生很大影响。

这种方法更适合大多数只需要初始用户位置修复并且仅在用户移动很长距离时才需要更新的应用程序。此界面仅在检测到与设备关联的信号塔发生变化时才会提供新事件,从而降低更新频率并显着降低功耗。

更多详情您可以查看CLLocationManager Guide

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多