我在swift 2.0中遇到了問題LocationManager
。它我的位置類:Swift 2.0 LocationManager是零
*
import Foundation
import CoreLocation
class Location : NSObject, CLLocationManagerDelegate {
var locationManager: CLLocationManager?
var coordinate: CLLocationCoordinate2D?
func locationManagerStart() {
if locationManager == nil {
locationManager = CLLocationManager()
locationManager?.delegate = self;
locationManager?.desiredAccuracy = kCLLocationAccuracyBest
locationManager?.requestWhenInUseAuthorization()
}
locationManager?.startUpdatingLocation()
}
func locationManagerStop() {
locationManager?.stopUpdatingLocation()
}
//MARK: CLLocationManagerDelegate
func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) {
coordinate = newLocation.coordinate
}
*
我在這個類中AppDelegate
創建對象:
let location = Location()
,並開始位置時我的應用程序變得活躍起來:
func applicationDidBecomeActive(application: UIApplication) {
location.locationManagerStart()
}
後來在我的代碼中,我需要檢查我是否有權訪問位置。我發售者,我有,因爲我對iPhone有位置,我接受了我的位置:
func haveAccessToLocation() -> Bool {
if (coordinate?.latitude) != nil {
return true
}else {
return false
}
}
我cordinate?.latitude/longitude
是零,我locationManager
爲零太,我不知道爲什麼
你正在模擬器上運行此? - 在真實的設備上測試它。 – Fennec
你是否檢查didUpdateToLocation是否被調用 –
當你第一次運行你的應用時,它是否顯示彈出來啓用位置? –