locationManager.location 始终为零
locationManager.location is always nil
我基本上已经从互联网上提取了这段代码:
//variables
//location manager
var locationManager:CLLocationManager!
var currentLocation:CLLocation?
//outlets
@IBOutlet weak var whatTextField: UITextField!
@IBOutlet weak var whereTextField: UITextField!
@IBOutlet weak var whenTextField: UITextField!
@IBAction func onCreateEventClick(_ sender: Any) {
let event = CAEvent(eventId: "123777abc", eventName: whatTextField.text, location: currentLocation)
event.save { (error) in
//handle event error
print(error)
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if( CLLocationManager.authorizationStatus() == .authorizedWhenInUse ||
CLLocationManager.authorizationStatus() == .authorizedAlways){
determineCurrentLocation()
}
}
func determineCurrentLocation() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
//locationManager.startUpdatingHeading()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
currentLocation = locations[0] as CLLocation
// Call stopUpdatingLocation() to stop listening for location updates,
// other wise this function will be called every time when user location changes.
// manager.stopUpdatingLocation()
print("user latitude = \(currentLocation?.coordinate.latitude)")
print("user longitude = \(currentLocation?.coordinate.longitude)")
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
{
print("Error \(error)")
}
起初我能够看到位置(例如,不是零)。但是现在,每次都是零。我已经尝试更改我的模拟器的位置,并且我已经确认我的模拟器中的应用程序正在共享位置。我还添加了对 startUpdatingLocation()
的调用并添加了委托 didUpdateLocations
并注意到 didUpdateLocationsAny
未被调用。还有其他想法吗?谢谢!
位置通常不会在请求后立即可用,一旦您将对象设置为委托,您应该实现 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
并将 currentLocation
设置为该数组的最后一个元素。
只要获取或更改位置,就会调用该方法。
你调用locationManager方法startUpdatingLocation()了吗?开始更新位置的最佳位置是 locationManager 委托方法:
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
switch(CLLocationManager.authorizationStatus()) {
case .authorizedAlways, .authorizedWhenInUse:
locationManager.startUpdatingLocation()
case .denied, .notDetermined, .restricted:
locationManager.stopUpdatingLocation()
}
}
我基本上已经从互联网上提取了这段代码:
//variables
//location manager
var locationManager:CLLocationManager!
var currentLocation:CLLocation?
//outlets
@IBOutlet weak var whatTextField: UITextField!
@IBOutlet weak var whereTextField: UITextField!
@IBOutlet weak var whenTextField: UITextField!
@IBAction func onCreateEventClick(_ sender: Any) {
let event = CAEvent(eventId: "123777abc", eventName: whatTextField.text, location: currentLocation)
event.save { (error) in
//handle event error
print(error)
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if( CLLocationManager.authorizationStatus() == .authorizedWhenInUse ||
CLLocationManager.authorizationStatus() == .authorizedAlways){
determineCurrentLocation()
}
}
func determineCurrentLocation() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
//locationManager.startUpdatingHeading()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
currentLocation = locations[0] as CLLocation
// Call stopUpdatingLocation() to stop listening for location updates,
// other wise this function will be called every time when user location changes.
// manager.stopUpdatingLocation()
print("user latitude = \(currentLocation?.coordinate.latitude)")
print("user longitude = \(currentLocation?.coordinate.longitude)")
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
{
print("Error \(error)")
}
起初我能够看到位置(例如,不是零)。但是现在,每次都是零。我已经尝试更改我的模拟器的位置,并且我已经确认我的模拟器中的应用程序正在共享位置。我还添加了对 startUpdatingLocation()
的调用并添加了委托 didUpdateLocations
并注意到 didUpdateLocationsAny
未被调用。还有其他想法吗?谢谢!
位置通常不会在请求后立即可用,一旦您将对象设置为委托,您应该实现 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
并将 currentLocation
设置为该数组的最后一个元素。
只要获取或更改位置,就会调用该方法。
你调用locationManager方法startUpdatingLocation()了吗?开始更新位置的最佳位置是 locationManager 委托方法:
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
switch(CLLocationManager.authorizationStatus()) {
case .authorizedAlways, .authorizedWhenInUse:
locationManager.startUpdatingLocation()
case .denied, .notDetermined, .restricted:
locationManager.stopUpdatingLocation()
}
}