我为CoreLocation的位置管理器实现了一个单例对象,我正在使用Swift 1.2及更高版本的类常量方法开发的应用程序here。
虽然当我尝试直接或使用getter方法访问currentLocation
变量时,我得到nil
。
我错过了什么?
实施
import Foundation
import CoreLocation
class LocationService: NSObject, CLLocationManagerDelegate {
static let sharedInstance = LocationService()
var locationManager: CLLocationManager!
var currentLocation: CLLocationCoordinate2D!
var currentDirection: StepDirection!
private override init() {
super.init()
locationManager = CLLocationManager()
locationManager.requestAlwaysAuthorization()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
locationManager.headingFilter = kCLHeadingFilterNone
locationManager.distanceFilter = 1
}
// MARK: Control Methods
func startUpdatingLocation() {
locationManager.startUpdatingLocation()
print("Location updates are started.")
}
func stopUpdatingLocation() {
locationManager.stopUpdatingLocation()
print("Location updates are stopped.")
}
func startUpdatingHeading() {
locationManager.startUpdatingHeading()
print("Compass updates are started.")
}
func stopUpdatingHeading() {
locationManager.stopUpdatingHeading()
print("Compass updates are stopped.")
}
// MARK: CoreLocation Location Updates
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// If location data can be determined
if let location = locations.last! as CLLocation! {
currentLocation = location.coordinate
// print("Current Location: \(currentLocation)")
NSNotificationCenter.defaultCenter().postNotificationName("LocationUpdate", object: self,
userInfo: ["longitude": currentLocation.longitude,
"latitude": currentLocation.latitude])
}
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Location Manager: \(error)")
NSNotificationCenter.defaultCenter().postNotificationName("LocationUpdateError", object: self,
userInfo: nil)
}
// MARK: CoreLocation Heading Updates
func locationManager(manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
let trueHeading = newHeading.trueHeading
var declanation = (newHeading.trueHeading - newHeading.magneticHeading)
declanation = 50.0
if (0.0+declanation <= trueHeading) && (trueHeading <= 90.0+declanation) {
currentDirection = StepDirection.Right
} else if (90.0+declanation < trueHeading) && (trueHeading <= 180.0+declanation) {
currentDirection = StepDirection.Down
} else if (180.0+declanation < trueHeading) && (trueHeading <= 270.0+declanation) {
currentDirection = StepDirection.Left
} else if (270.0+declanation < trueHeading) && (trueHeading <= 360.0+declanation) {
currentDirection = StepDirection.Up
}
NSNotificationCenter.defaultCenter().postNotificationName("CompassUpdate", object: self, userInfo: ["currentDirection": currentDirection.rawValue])
}
func locationManagerShouldDisplayHeadingCalibration(manager: CLLocationManager) -> Bool {
return true
}
// MARK: Access Methods
func getCurrentLocation() -> CLLocationCoordinate2D! {
return currentLocation
}
}
使用
我首先尝试访问它,如下所示:
LocationService.sharedInstance.currentLocation
或LocationService.sharedInstance.getCurrentLocation
然后我将共享实例分配给变量,认为我没有保留状态:
locationService = LocationService.sharedInstance
然后使用访问方法或变量名称:
locationService.currentLocation
或locationService.getCurrentLocation
答案 0 :(得分:1)
您需要调用startUpdating函数以让位置管理器开始更新位置。
private override init() {
super.init()
locationManager = CLLocationManager()
locationManager.requestAlwaysAuthorization()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
locationManager.headingFilter = kCLHeadingFilterNone
locationManager.distanceFilter = 1
locationManager.startUpdatingLocation()
}
正如您在评论中所说,位置管理员将不断尝试获取用户的位置,直到调用停止更新为止。
讨论 此方法立即返回。调用此方法会导致位置管理器获取初始位置修复(可能需要几秒钟),并通过调用其locationManager:didUpdateLocations:方法来通知您的委托。之后,接收器主要在超出distanceFilter属性值时生成更新事件。但是,更新可能会在其他情况下提供。例如,如果硬件收集更准确的位置读数,接收器可以发送另一个通知。
您可以使用位置管理器的位置属性(您已在委托方法中执行)从委托方法访问新的位置数据。