我正在尝试使用CLLocationManager计算行进距离并显示标签内部的距离。请求使用位置工作但标签中没有显示任何内容。我已经尝试过模拟xCode模拟器内部的驱动器,但无济于事。我有一个显示在应用程序内部的地图,并且有效,它可以跟踪我的位置,但这些都有效。
继承我的locationManager方法:
func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]) {
// Map
theLabel.text = "\(locations[0])"
myLocations.append(locations[0] as! CLLocation)
let spanX = 0.007
let spanY = 0.007
let newRegion = MKCoordinateRegion(center: theMap.userLocation.coordinate, span: MKCoordinateSpanMake(spanX, spanY))
theMap.setRegion(newRegion, animated: true)
if (myLocations.count > 1){
let sourceIndex = myLocations.count - 1
let destinationIndex = myLocations.count - 2
let c1 = myLocations[sourceIndex].coordinate
let c2 = myLocations[destinationIndex].coordinate
var a = [c1, c2]
let polyline = MKPolyline(coordinates: &a, count: a.count)
theMap.add(polyline)
}
// Labels
let latestLocation: AnyObject = locations[locations.count - 1]
lat.text = String(format: "%.4f", latestLocation.coordinate.latitude)
long.text = String(format: "%.4f", latestLocation.coordinate.longitude)
horizAcc.text = String(format: "%.4f", latestLocation.horizontalAccuracy)
altitude.text = String(format: "%.4f", latestLocation.altitude)
vertAcc.text = String(format: "%.4f", latestLocation.verticalAccuracy)
if startLocation == nil {
startLocation = latestLocation as! CLLocation
}
let distanceBetween = (latestLocation.distance(from: startLocation))
distance.text = String(format: "%.2f", distanceBetween)
}
非常感谢任何帮助!如果需要,我可以提供更多代码。
答案 0 :(得分:0)
听起来您没有为CLLocationManager的实例正确设置委托属性。您需要让它知道哪个对象包含CLLocationManagerDelegate方法的实现,就像您列出的方法一样。
因此,假设您的视图控制器符合CLLocationManagerDelegate
并包含您发布的方法,并假设您已经为视图控制器CLLocationManager
提供了名为“locationManager”的属性,则只需将其设置为:< / p>
self.locationManager.delegate = self
self.locationManager.startUpdatingLocation()
您可以将它放在viewDidLoad:或适合您需要的任何地方,并且应该调用您的didUpdateLocation委托方法。