gpt4 book ai didi

ios - 没有互联网连接的位置

转载 作者:可可西里 更新时间:2023-10-31 23:44:24 24 4
gpt4 key购买 nike

我想在按下按钮时获取用户的位置,我找到了一种使用 CLLocationManager 的方法,该代码可以完美运行,直到设备没有网络连接,函数 func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 捕获错误

((NSError?) error = domain: "kCLErrorDomain" - code: 2 { _userInfo = nil })

如何在没有互联网连接的情况下获取设备位置?这是我使用的代码

import CoreLocation

class SesionViewController: UIViewController, CLLocationManagerDelegate {

let locationManager = CLLocationManager()

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: { (placemarks, error) -> Void in
if (error != nil) {
print("Error:" + error!.localizedDescription)
return
}
if placemarks!.count > 0 {
let pm = placemarks![0] as CLPlacemark
print(pm!.coordinate.latitude)
}else {
print("Error with data")
}

})
}
func startButtonPress(sender:UIButton!){
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.locationManager.stopUpdatingLocation()
}
}

编辑

我已经找到了解决方案,我只是不执行 CLGeocoder().reverseGeocodeLocation... 只是像这样用 locations var 捕获位置

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(manager.location!.coordinate.latitude)
}

最佳答案

来自关于 ClGeocoder 的 Apple 文档

The computer or device must have access to the network in order for the geocoder object to return detailed placemark information

CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: { (placemarks, error) -> Void in
if (error != nil) {
print("Error:" + error!.localizedDescription)
return
}
if placemarks!.count > 0 {
let pm = placemarks![0] as CLPlacemark
print(pm!.coordinate.latitude)
}else {
print("Error with data")
}

})

在没有互联网访问的情况下总是会报告错误。您的位置数据仍然可用(如果您的设备有 GPS 装置)

关于ios - 没有互联网连接的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33960274/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com