【发布时间】: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