【发布时间】:2016-12-24 15:52:28
【问题描述】:
因此,在我目前的project 中,我正在做一种计算与普通汽车相比驾驶轻便摩托车时节省的排放量的方法。该函数包含两部分,method(计算)和tracker function。主要问题是跟踪器功能以某种方式似乎根本不跟踪。
我的主要问题是,如何让跟踪器功能在应用打开时始终跟踪?
这是追踪功能
var startLocation:CLLocation!
var lastLocation: CLLocation!
var traveledDistance:Double = 0
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if startLocation == nil {
startLocation = locations.first
} else {
if let lastLocation = locations.last {
let distance = startLocation.distanceFromLocation(lastLocation)
let lastDistance = lastLocation.distanceFromLocation(lastLocation)
traveledDistance += lastDistance
print( "\(startLocation)")
print( "\(lastLocation)")
print("FULL DISTANCE: \(traveledDistance)")
print("STRAIGHT DISTANCE: \(distance)")
var travelDistance = setData("distance")
}
}
lastLocation = locations.last
}
这就是方法
func calculateEmission(numbers: Int...) -> Double{
let recordedDistance = getData("distance")
let dis = recordedDistance
let emissionAve = 0.16
let calculatedEmission : Double = Double(dis) * Double(emissionAve)
print(calculatedEmission, "kg Co2")
return calculatedEmission
}
【问题讨论】:
-
是什么让你相信应该调用这个方法?您是否在某处创建了
CLLocationManager,是否将其设置为delegate为包含此方法的任何类的实例? -
这个问题还是很模糊的。请准确说明您要查找的信息。
-
是否提示您授予位置服务访问权限?
-
@Sethmr 对不起!写这个问题时有点分心,但我的问题是“如何让跟踪器功能在应用程序打开时始终跟踪”?
-
@MwcsMac 我会看看是否能解决问题。如果不是,可能是因为我真的不知道在哪里/何时调用跟踪器函数。
标签: swift function methods distance tracking