来自 locationManager didUpdateLocations 的 CLLocationCoordinates
CLLocationCoordinates from locationManager didUpdateLocations
我正在尝试使用 CoreLocation(一旦获得许可)来获取用户的 CLLocationCoordinate2D,以便我可以将该信息传递给 Uber 深层链接(在按下我的 TableView 中的 UIButton 之后)。
我已经想出如何获取坐标作为 CLLocations,并在 didUpdateLocations 方法中将它们转换为 CLLocationCoordinates2D,但似乎无法将它们传输到我的 buttonPressed 函数。
谁能解释一下如何将坐标信息正确传输到 uberButtonPressed 方法?我也对如何让 locationManager 在确定合适的位置后停止更新位置感到困惑。任何帮助深表感谢。顺便说一句,我正在使用它来实例化 Uber:https://github.com/kirby/uber
import UIKit
import CoreLocation
import MapKit
class ATableViewController: UITableViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
var location: CLLocation?
var coordinate: CLLocationCoordinate2D?
// Implemented tableView methods etc here...
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let newLocation = locations.last as! CLLocation
println("DID UPDATE LOCATIONS \(newLocation)")
location = newLocation
coordinate = location!.coordinate
println("WE HAVE THE COORDINATES \(coordinate!.latitude) and \(coordinate!.longitude)") // this prints along with DID UPDATE LOCATIONS
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println("error:" + error.localizedDescription)
}
func uberButtonPressed(sender: UIButton!) {
let senderButton = sender
println(senderButton.tag)
let authStatus: CLAuthorizationStatus = CLLocationManager.authorizationStatus()
if authStatus == .NotDetermined {
locationManager.requestWhenInUseAuthorization()
return
}
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
var pickupLocation = coordinate! // fatal error: unexpectedly found nil while unwrapping an Optional value
println(pickupLocation)
// // Create an Uber instance
// var uber = Uber(pickupLocation: pickupLocation)
//
// Set a few optional properties
// uber.pickupNickname = "OK"
//
// uber.dropoffLocation = CLLocationCoordinate2D(latitude: 47.591351, longitude: -122.332271)
// uber.dropoffNickname = "whatever"
//
// // Let's do it!
// uber.deepLink()
//
}
问题是您正在展开 coordinate
,它是 CLLocationCoordinate2D?
和 !
。由于坐标来自 CLLocation?
的位置。坐标为零且位置为零是可能的,尤其是在位置服务有机会启动之前。对于坐标和位置不为零的情况,仅 运行 uberButtonPressed:
的其余部分通过使用if let currentCoordinate = coordinate?
,在这种情况下,请调用 locationManager.stopUpdatingLocation()
。
您应该将 var pickupLocation = coordinate!
移到您的 didUpdateLocations
中。分配后,您也可以从 'didUpdateLocation
内部调用 locationManager.stopUpdatingLocation()
或您的坐标值并不断更新。停止 locationManager 后,调用新函数 运行 当前在 func uberButtonPressed
中的其余代码
我遇到了同样的问题。
而不是像这样调用委托方法:
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
debugPrint("NOT CALLED-- didUpdateLocations AnyObject")
}
我变成了:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
debugPrint("CALLED-- didUpdateLocations CLLocation")
}
我正在尝试使用 CoreLocation(一旦获得许可)来获取用户的 CLLocationCoordinate2D,以便我可以将该信息传递给 Uber 深层链接(在按下我的 TableView 中的 UIButton 之后)。
我已经想出如何获取坐标作为 CLLocations,并在 didUpdateLocations 方法中将它们转换为 CLLocationCoordinates2D,但似乎无法将它们传输到我的 buttonPressed 函数。
谁能解释一下如何将坐标信息正确传输到 uberButtonPressed 方法?我也对如何让 locationManager 在确定合适的位置后停止更新位置感到困惑。任何帮助深表感谢。顺便说一句,我正在使用它来实例化 Uber:https://github.com/kirby/uber
import UIKit
import CoreLocation
import MapKit
class ATableViewController: UITableViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
var location: CLLocation?
var coordinate: CLLocationCoordinate2D?
// Implemented tableView methods etc here...
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let newLocation = locations.last as! CLLocation
println("DID UPDATE LOCATIONS \(newLocation)")
location = newLocation
coordinate = location!.coordinate
println("WE HAVE THE COORDINATES \(coordinate!.latitude) and \(coordinate!.longitude)") // this prints along with DID UPDATE LOCATIONS
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println("error:" + error.localizedDescription)
}
func uberButtonPressed(sender: UIButton!) {
let senderButton = sender
println(senderButton.tag)
let authStatus: CLAuthorizationStatus = CLLocationManager.authorizationStatus()
if authStatus == .NotDetermined {
locationManager.requestWhenInUseAuthorization()
return
}
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
var pickupLocation = coordinate! // fatal error: unexpectedly found nil while unwrapping an Optional value
println(pickupLocation)
// // Create an Uber instance
// var uber = Uber(pickupLocation: pickupLocation)
//
// Set a few optional properties
// uber.pickupNickname = "OK"
//
// uber.dropoffLocation = CLLocationCoordinate2D(latitude: 47.591351, longitude: -122.332271)
// uber.dropoffNickname = "whatever"
//
// // Let's do it!
// uber.deepLink()
//
}
问题是您正在展开 coordinate
,它是 CLLocationCoordinate2D?
和 !
。由于坐标来自 CLLocation?
的位置。坐标为零且位置为零是可能的,尤其是在位置服务有机会启动之前。对于坐标和位置不为零的情况,仅 运行 uberButtonPressed:
的其余部分通过使用if let currentCoordinate = coordinate?
,在这种情况下,请调用 locationManager.stopUpdatingLocation()
。
您应该将 var pickupLocation = coordinate!
移到您的 didUpdateLocations
中。分配后,您也可以从 'didUpdateLocation
内部调用 locationManager.stopUpdatingLocation()
或您的坐标值并不断更新。停止 locationManager 后,调用新函数 运行 当前在 func uberButtonPressed
我遇到了同样的问题。
而不是像这样调用委托方法:
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
debugPrint("NOT CALLED-- didUpdateLocations AnyObject")
}
我变成了:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
debugPrint("CALLED-- didUpdateLocations CLLocation")
}