【发布时间】:2016-08-18 21:18:28
【问题描述】:
在我的DC Metro tracking application 中,我使用CoreLocation 选择离用户最近的地铁站,并显示他们附近的车站列表。
它在 macOS 10.11 中完美运行,但我无法让它在 macOS 10.10 上运行。为了调试它,我在TodayViewController.swift 的locationManager(_:didUpdateLocations:) 方法中插入了一行来打印应用程序获取的位置。
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject]) {
LocationManager.sharedManager.stopUpdatingLocation()
print(LocationManager.sharedManager.location)
...
}
在 El Capitan 上,这会将以下内容输出到控制台(成功):
Optional(<+38.92208178,-77.22920176> +/- 65.00m (speed -1.00 mps / course -1.00) @ 8/18/16, 5:04:37 PM Eastern Daylight Time)
在 Yosemite 上,它只输出 nil。
我也尝试过模拟我的位置,但无济于事。
有人有什么建议吗? Yosemite 机器上启用了定位服务,我知道它正在工作,因为天气通知中心小部件正在正确获取其位置。
谢谢!
更多相关代码:
override func viewWillAppear() {
super.viewWillAppear()
...
switch CLLocationManager.authorizationStatus() {
case .Authorized:
if !didSelectStation {
selectedStationLabel.stringValue = "Determining closest station..."
}
LocationManager.sharedManager.startUpdatingLocation()
case .NotDetermined:
getCurrentLocationButton.hidden = false
mainPredictionView.hidden = true
default: // Denied or Restricted
WMATAfetcher.getPredictionsForSelectedStation()
}
}
class LocationManager {
static let sharedManager: CLLocationManager = {
let locationManager = CLLocationManager()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = 100.0
return locationManager
}()
}
我还有一个按钮,getCurrentLocationButton,它调用startUpdatingLocation()
@IBAction func getCurrentLocation(sender: NSButton) {
LocationManager.sharedManager.startUpdatingLocation()
}
【问题讨论】:
-
您在致电
stopUpdatingLocation()之前尝试过打印吗?另外,你实现locationManager(_:didFailWithError:)委托方法了吗? -
是的,对不起,我忘了提到我确实实现了
locationManager(_:didFailWithError:)委托方法。我认为自从我在locationManager(_:didUpdateLocations:)中对print()的调用执行后,我不必担心它。作为健全性检查,我在该方法中添加了print()语句,它在运行时没有执行。谢谢你,@Deyton! -
您最初的建议已解决!当我将
LocationManager.sharedManager.stopUpdatingLocation()调用移到print(LocationManager.sharedManager.location)之后时,它成功显示了当前位置。再次感谢@Deyton! -
太棒了!我会将该建议移至答案。
标签: swift macos core-location cllocationmanager