计算iOS Swift的总行进距离

计算iOS Swift的总行进距离,第1张

概述如何计算在 Swift中使用CoreLocation的总行进距离 到目前为止,我还没有找到任何有关如何在Swift for iOS 8中执行此 *** 作的资源, 您如何计算自开始追踪您的位置以来移动的总距离? 从我到目前为止所读到的,我需要保存点的位置,然后计算当前点和最后一点之间的距离,然后将该距离添加到totalDistance变量 Objective-C对我来说是非常陌生的,所以我无法解决swif 如何计算在 Swift中使用CoreLocation的总行进距离

到目前为止,我还没有找到任何有关如何在Swift for iOS 8中执行此 *** 作的资源,

您如何计算自开始追踪您的位置以来移动的总距离?

从我到目前为止所读到的,我需要保存点的位置,然后计算当前点和最后一点之间的距离,然后将该距离添加到totaldistance变量

Objective-C对我来说是非常陌生的,所以我无法解决swift语法问题

这是我到目前为止所做的,不确定我是否做得对.虽然distanceFromLocation方法返回所有0.0所以显然有些错误

func locationManager(manager: CLLocationManager!,dIDUpdateLocations locations: [AnyObject]!) {     var newLocation: CLLocation = locations[0] as CLLocation    oldLocationArray.append(newLocation)           var totaldistance = CLLocationdistance()    var oldLocation = oldLocationArray.last    var distanceTraveled = newLocation.distanceFromLocation(oldLocation)    totaldistance += distanceTraveled println(distanceTraveled)}
解决方法 更新:Xcode 8.3.2•Swift 3.1

问题在于因为你总是一遍又一遍地获得相同的位置.试试这样:

import UIKitimport MapKitclass VIEwController: UIVIEwController,CLLocationManagerDelegate {    @IBOutlet weak var mapVIEw: MKMapVIEw!    let locationManager = CLLocationManager()    var startLocation: CLLocation!    var lastLocation: CLLocation!    var startDate: Date!    var traveleddistance: Double = 0    overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()        if CLLocationManager.locationServicesEnabled() {            locationManager.delegate = self            locationManager.desiredAccuracy = kCLLocationAccuracyBest            locationManager.requestWhenInUseAuthorization()            locationManager.startUpdatingLocation()            locationManager.startMonitoringSignificantLocationChanges()            locationManager.distanceFilter = 10            mapVIEw.showsUserLocation = true            mapVIEw.userTrackingMode = .follow        }    }    func locationManager(_ manager: CLLocationManager,dIDUpdateLocations locations: [CLLocation]) {        if startDate == nil {            startDate = Date()        } else {            print("elapsedtime:",String(format: "%.0fs",Date().timeIntervalSince(startDate)))        }        if startLocation == nil {            startLocation = locations.first        } else if let location = locations.last {            traveleddistance += lastLocation.distance(from: location)            print("Traveled distance:",traveleddistance)            print("Straight distance:",startLocation.distance(from: locations.last!))        }        lastLocation = locations.last    }    func locationManager(_ manager: CLLocationManager,dIDFailWithError error: Error) {        if (error as? CLError)?.code == .denIEd {            manager.stopUpdatingLocation()            manager.stopMonitoringSignificantLocationChanges()        }    }}

Sample Project

总结

以上是内存溢出为你收集整理的计算iOS Swift的总行进距离全部内容,希望文章能够帮你解决计算iOS Swift的总行进距离所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1109372.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存