我想取出委托中获取的坐标
I want to take out the coordinates acquired in the delegate
我编写了一个程序,只要当前位置发生变化,它就会使用 LocationManagerDelegate 在调试区域显示坐标。检索坐标时出错
不能在 属性 初始值设定项中使用实例成员 'locationManager'; 属性 初始值设定项 运行 在 'self' 可用之前
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate{
var locationManager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
setUpLocationManager()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setUpLocationManager() {
locationManager = CLLocationManager()
guard let locationManager = locationManager else {return}
locationManager.requestWhenInUseAuthorization()
let status = CLLocationManager.authorizationStatus()
if status == .authorizedWhenInUse {
locationManager.delegate = self
locationManager.distanceFilter = 10
locationManager.startUpdatingLocation()
printLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) ->Optional<Any> {
let location = locations.first
let latitude = location?.coordinate.latitude
let longitude = location?.coordinate.longitude
let latlong = [latitude, longitude]
return latlong
}
let myLocation = locationManager()
func printLocation() {
print("test\(myLocation)")
}
}
测试(函数)
是输出
let myLocation = locationManager ()
当你改成
let myLocation = locationManager
您的代码包含一些错误。
发生错误是因为您无法在 class 的顶层执行受影响的行。
首先,您不得更改委托方法的签名。这个自定义委托方法
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) ->Optional<Any> { ...
将永远不会被调用。
除此之外,为什么您将 return 类型声明为 Any?
,尽管它应该是 [CLLocationCoordinate2D]
?
立即创建位置管理器,替换
var locationManager: CLLocationManager!
和
let locationManager = CLLocationManager()
在setUpLocationManager()
中删除行
locationManager = CLLocationManager()
guard let locationManager = locationManager else {return} // this line is completely pointless anyway
printLocation()
委托方法didUpdateLocations
被周期性地异步地调用。在方法中打印结果
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.first else { return }
let latitude = location.coordinate.latitude
let longitude = location.coordinate.longitude
let latlong = [latitude, longitude]
print("test", latlong)
}
删除
let myLocation = locationManager()
func printLocation() {
print("test\(myLocation)")
}
我编写了一个程序,只要当前位置发生变化,它就会使用 LocationManagerDelegate 在调试区域显示坐标。检索坐标时出错
不能在 属性 初始值设定项中使用实例成员 'locationManager'; 属性 初始值设定项 运行 在 'self' 可用之前
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate{
var locationManager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
setUpLocationManager()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setUpLocationManager() {
locationManager = CLLocationManager()
guard let locationManager = locationManager else {return}
locationManager.requestWhenInUseAuthorization()
let status = CLLocationManager.authorizationStatus()
if status == .authorizedWhenInUse {
locationManager.delegate = self
locationManager.distanceFilter = 10
locationManager.startUpdatingLocation()
printLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) ->Optional<Any> {
let location = locations.first
let latitude = location?.coordinate.latitude
let longitude = location?.coordinate.longitude
let latlong = [latitude, longitude]
return latlong
}
let myLocation = locationManager()
func printLocation() {
print("test\(myLocation)")
}
}
测试(函数) 是输出
let myLocation = locationManager ()
当你改成
let myLocation = locationManager
您的代码包含一些错误。
发生错误是因为您无法在 class 的顶层执行受影响的行。
首先,您不得更改委托方法的签名。这个自定义委托方法
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) ->Optional<Any> { ...
将永远不会被调用。
除此之外,为什么您将 return 类型声明为 Any?
,尽管它应该是 [CLLocationCoordinate2D]
?
立即创建位置管理器,替换
var locationManager: CLLocationManager!
和
let locationManager = CLLocationManager()
在
setUpLocationManager()
中删除行locationManager = CLLocationManager() guard let locationManager = locationManager else {return} // this line is completely pointless anyway printLocation()
委托方法
didUpdateLocations
被周期性地异步地调用。在方法中打印结果func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { guard let location = locations.first else { return } let latitude = location.coordinate.latitude let longitude = location.coordinate.longitude let latlong = [latitude, longitude] print("test", latlong) }
删除
let myLocation = locationManager() func printLocation() { print("test\(myLocation)") }