我有以下代码
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
var coordinates: String?
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//updates the user's location as the user moves
let location: CLLocation = locations[0] as CLLocation
coordinates = "\(location.coordinate.latitude),\(location.coordinate.longitude)"
print(coordinates!)
}
当我在模拟器上运行它时,程序工作正常,但它不打印坐标 . 当我在iPhone上运行它时,它会打印坐标 . 我需要更改Xcode设置中的某些内容来修复此问题吗?此外,当我尝试在另一个函数中使用“coordinates”变量时,我得到一个错误,说即使我在locationManager函数中为它分配了一个值,编译器也会找到nil,所以我猜这两个问题是连接的 . 提前致谢
1 回答
您可以在运行应用时使用“模拟位置”按钮模拟模拟器上的位置:
您可以选择其中一个现有位置,也可以创建GPX文件(例如,不是静态单点,而是可以模拟不断更改位置的路径) . 我建议你使用https://mapstogpx.com/来创建GPX文件 .