【发布时间】:2016-07-07 20:56:40
【问题描述】:
我正在尝试获取用户的纬度和经度。
import Foundation
import CoreLocation
class locationModule: NSObject, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
func getLocation() {
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
var location: CLLocationCoordinate2D = (manager.location?.coordinate)!
print(location.latitude)
print(location.longitude)
}
}
这是代码在一个单独的 Swift 文件中,所以我需要 getLocation() 才能调用 func locationManager,我将如何从 getLocation() 函数调用 locationManager 函数。
【问题讨论】:
-
你为什么要打电话给
locationManager?重点是它被CLLocationManager为你调用 -
@AMomchilov 什么叫
CLLocationManager -
CLLocationManager的全部意义在于它调用你的CLLocationManagerDelegate的locationManager方法,以便通知你位置变化。自己调用该方法没有意义。
标签: swift location core-location cllocation