Xcode 7.1 iOS 9.1 CLLocationManager 不总是工作?
Xcode 7.1 iOS 9.1 CLLocationManager not always working?
在实现 CLLocationManager 时我遇到了一个问题。有时,在 iPhone 模拟器中启动应用程序时,它只是不获取当前位置。但是当我重新启动应用程序时,或者它在 1-3 次重新启动后突然工作。重新启动 Xcode 也可以...这是我的代码:
private var lat = 0.0;
private var long = 0.0;
private var didFindLocation = false
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if (status == .AuthorizedAlways) || (status == .AuthorizedWhenInUse) {
didFindLocation = false
locationManager.startUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last! as CLLocation
self.lat = location.coordinate.latitude
self.long = location.coordinate.longitude
if !didFindLocation {
print("\(self.lat) - \(self.long)")
didFindLocation = true
locationManager.stopUpdatingLocation()
}
}
如您所见,我创建了一个布尔值 didFindLocation
,它允许我只获取一次位置。我设置了一些断点来查看发生了什么,当位置没有被获取时,func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
甚至没有被调用。
谢谢
这可能发生在模拟器上,但请添加此方法,以便您收到一条错误消息,说明为什么它在模拟器上无法正常工作
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print(error.localizedDescription)
}
iOS 模拟器不包含加速度计、陀螺仪等传感器,因此我们不应该期望在模拟器上使用它们。除此之外,为了获取位置,它可能正在使用您系统的互联网。
因此,为了获得公平的结果,建议在此类情况下使用真实设备。
从编码的角度来看,您可以检查 CCLocationManager 的 locationServicesEnabled
属性 来查看此服务是否已启用。
当您在模拟器中检查位置时,首先您需要将模拟器指向某个位置。
为什么?
因为模拟器不是实时设备,所以首先你需要将它指向某个位置。
怎么做?
有两种方法。
1) select 模拟器 -> 转到调试 -> select 位置 -> select 苹果位置。
您调用的定位方法
Noto : if you select custom location from menu and enter your custom location then you need to restart your simulator (some time multiple times)
2) 转到 xcode -> 运行 你的项目 -> 在调试面板中你找到一个位置图标 -> 点击它 -> 它显示位置名称 -> select任意地名
Note : Not necessary it worked all the times.
在实现 CLLocationManager 时我遇到了一个问题。有时,在 iPhone 模拟器中启动应用程序时,它只是不获取当前位置。但是当我重新启动应用程序时,或者它在 1-3 次重新启动后突然工作。重新启动 Xcode 也可以...这是我的代码:
private var lat = 0.0;
private var long = 0.0;
private var didFindLocation = false
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if (status == .AuthorizedAlways) || (status == .AuthorizedWhenInUse) {
didFindLocation = false
locationManager.startUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last! as CLLocation
self.lat = location.coordinate.latitude
self.long = location.coordinate.longitude
if !didFindLocation {
print("\(self.lat) - \(self.long)")
didFindLocation = true
locationManager.stopUpdatingLocation()
}
}
如您所见,我创建了一个布尔值 didFindLocation
,它允许我只获取一次位置。我设置了一些断点来查看发生了什么,当位置没有被获取时,func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
甚至没有被调用。
谢谢
这可能发生在模拟器上,但请添加此方法,以便您收到一条错误消息,说明为什么它在模拟器上无法正常工作
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print(error.localizedDescription)
}
iOS 模拟器不包含加速度计、陀螺仪等传感器,因此我们不应该期望在模拟器上使用它们。除此之外,为了获取位置,它可能正在使用您系统的互联网。
因此,为了获得公平的结果,建议在此类情况下使用真实设备。
从编码的角度来看,您可以检查 CCLocationManager 的 locationServicesEnabled
属性 来查看此服务是否已启用。
当您在模拟器中检查位置时,首先您需要将模拟器指向某个位置。 为什么?
因为模拟器不是实时设备,所以首先你需要将它指向某个位置。
怎么做?
有两种方法。
1) select 模拟器 -> 转到调试 -> select 位置 -> select 苹果位置。
您调用的定位方法
Noto : if you select custom location from menu and enter your custom location then you need to restart your simulator (some time multiple times)
2) 转到 xcode -> 运行 你的项目 -> 在调试面板中你找到一个位置图标 -> 点击它 -> 它显示位置名称 -> select任意地名
Note : Not necessary it worked all the times.