为什么这段代码不起作用?我想这是用didUpdateLocations高估方法的东西?我如何正确地将它们与我的标签连接起来并使它们成为现场:)
import UIKit
import CoreLocation
class MainVC: UIViewController, CLLocationManagerDelegate {
var walking = false
var pause = false
var locationManager: CLLocationManager = CLLocationManager()
var startLocation: CLLocation!
var speed: CLLocationSpeed = CLLocationSpeed()
@IBOutlet weak var latitudeLabel: UILabel!
@IBOutlet weak var longitudeLabel: UILabel!
@IBOutlet weak var horizontalAccuracyLabel: UILabel!
@IBOutlet weak var verticalAccuracyLabel: UILabel!
@IBOutlet weak var distanceLabel: UILabel!
@IBOutlet weak var altitudeLabel: UILabel!
@IBOutlet weak var speedLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
locationManager.startUpdatingHeading()
startLocation = nil
// Ask for Authorisation from the User.
self.locationManager.requestAlwaysAuthorization()
// For use in foreground
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func startDistance(_ sender: UIButton) {
startLocation = nil
walking = true
}
@IBAction func stopDistance(_ sender: UIButton) {
walking = false
}
@IBAction func resetDistance(_ sender: UIButton) {
startLocation = nil
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let latestLocation: AnyObject = locations[locations.count - 1]
latitudeLabel.text = String(format: "%.4f",latestLocation.coordinate.latitude)
longitudeLabel.text = String(format: "%.4f",latestLocation.coordinate.longitude)
horizontalAccuracyLabel.text = String(format: "%.4f",latestLocation.horizontalAccuracy)
verticalAccuracyLabel.text = String(format: "%.4f",latestLocation.verticalAccuracy)
altitudeLabel.text = String(format: "%.4f",latestLocation.altitude)
if walking == true {
if startLocation == nil {
startLocation = latestLocation as! CLLocation
speed = locationManager.location!.speed
speedLabel.text = String(format: "%.0f km/h", speed * 3.6)
}
let distanceBetween: CLLocationDistance =
latestLocation.distance(from: startLocation)
distanceLabel.text = String(format: "%.2f", distanceBetween)
}
}
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading)
{
// capLabel.text = String(format: "%.4f",newHeading.magneticHeading)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
}
}
我还在info.plist中添加了:Location Always Usage Description 我也在模拟器的设置中允许位置。
谢谢。
答案 0 :(得分:0)
要在 Swift 4 中始终为我可以看到您已在代码self.locationManager.requestAlwaysAuthorization()
中编写的位置服务配置授权,请执行以下操作:
您的info.plist
<key>NSLocationAlwaysUsageDescription</key>
<string> location permission text.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string> location permission text.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string> location permission text.</string>
查看文档here
您需要包含 NSLocationWhenInUseUsageDescription 和应用中的 NSLocationAlwaysAndWhenInUsageDescription 键 Info.plist文件。 (如果您的应用支持iOS 10及更早版本,那么 NSLocationAlwaysUsageDescription 键也是必需的。)如果这些键 不存在,授权请求立即失败。