为什么此代码不起作用?我猜这与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中的“位置始终使用情况描述”
我也允许在模拟器的“设置”中使用“位置信息”。

谢谢。

最佳答案

要在Swift 4中为位置服务配置始终授权,我可以看到您已经在代码self.locationManager.requestAlwaysAuthorization()中编写了该代码,请执行以下操作:

info.plist中还需要一个新的密钥NSLocationAlwaysAndWhenInUsageDescription

<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键。)如果这些键
  不存在,授权请求将立即失败。


因此,我们现在只有一个提示。
ios - Swift 4 CoreLocation无法正常工作-LMLPHP

关于ios - Swift 4 CoreLocation无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46626131/

10-14 21:56