所以在我当前的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
}
答案 0 :(得分:1)
确保info.plist中包含以下内容。然后,系统会提示您允许访问位置服务。
<key>NSLocationAlwaysUsageDescription</key>
<string>Needs access to access GPS</string>
<key>NSLocationUsageDescription</key>
<string>Needs access to access GPS</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Needs access to access GPS</string>
你应该在viewDidLoad中有这样的东西。
override func viewDidLoad() {
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
}