快速步行距离计算

时间:2017-09-21 12:24:07

标签: swift swift3 distance core-location pedometer

我想在点击按钮后计算步行距离。我使用CMPedometer进行应用但我不想使用CMPedometer。我会用位置信息来制作它。我使用了核心位置框架,但我不相信这个变量。我与你分享我的代码

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate  {

    var backgroundTaskIdentifier: UIBackgroundTaskIdentifier?
    override func viewDidLoad() {
        super.viewDidLoad()

        //locationManager.delegate = self
        if NSString(string:UIDevice.current.systemVersion).doubleValue > 8 {
            locationManager.requestAlwaysAuthorization()
        }
        locationManager.desiredAccuracy=kCLLocationAccuracyBest

        backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(expirationHandler: {
            UIApplication.shared.endBackgroundTask(self.backgroundTaskIdentifier!)
        })

        var timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.update), userInfo: nil, repeats: true)

    }

    func update() {
        var speed: CLLocationSpeed = CLLocationSpeed()
        speed = locationManager.location!.speed
        print(String(format: "%.0f km/h", speed * 3.6))
        latitute.text = String(format: "%.0f km/h", speed * 3.6)
    }

    @IBAction func resetDistance(_ sender: Any) {
        startLocation = nil
    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        var speed: CLLocationSpeed = CLLocationSpeed()
        speed = (locationManager.location?.speed)!

        latitute.text =  String(format: "%.0f km/h", speed * 3.6)
        print(speed);
    }

    func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        if status != CLAuthorizationStatus.denied{
            locationManager.startUpdatingLocation()
        }
    }






    @IBOutlet weak var latitute: UILabel!
    @IBOutlet weak var longitude: UILabel!
    @IBOutlet weak var horizontalAccuracy: UILabel!
    @IBOutlet weak var altitude: UILabel!
    @IBOutlet weak var verticalAccuracy: UILabel!
    @IBOutlet weak var distance: UILabel!

    var locationManager: CLLocationManager = CLLocationManager()
    var startLocation: CLLocation!
}

class Utility {

    class func convertCLLocationDistanceToMiles ( targetDistance : CLLocationDistance?) -> CLLocationDistance {
        var targetDistance = targetDistance
        targetDistance =  targetDistance!*0.00062137
        return targetDistance!
    }
    class func convertCLLocationDistanceToKiloMeters ( targetDistance : CLLocationDistance?) -> CLLocationDistance {
        var targetDistance = targetDistance
        targetDistance =  targetDistance!/1000
        return targetDistance!
    }

}

请您正确查看此代码是否正常工作。 我希望能帮到我

0 个答案:

没有答案