ios - 来自 locationManager didUpdateLocations 的 CLLocationCoordinates

标签 ios swift cllocationmanager cllocationcoordinate2d uber-api

我正在尝试使用 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()
//            
}

最佳答案

您应该将 var pickupLocation = coordinate! 移动到您的 didUpdateLocations 中。分配后,您也可以从 'didUpdateLocation 或您的坐标值中调用 locationManager.stopUpdatingLocation() 以保持更新。停止 locationManager 后,调用一个新函数来运行当前在 func uberButtonPressed

中的其余代码

关于ios - 来自 locationManager didUpdateLocations 的 CLLocationCoordinates,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29807933/

相关文章:

ios - 访问场景编辑器中定义的 Action

ios - 在加载 WKWebView 之前尝试获取 CLLocation 坐标

iOS地理围栏;这会比我现在拥有的更准确吗?

ios - Swift:获取用户的位置坐标作为纬度和经度?

ios - 与 NSDate 比较相关的核心数据谓词

c++ - 为 arm64 iPhone 编译 xcode 5 失败

ios - 为什么 "self"在我的 QueryOperation 中阻止来自 CloudKit 的数据

Swift:使用可失败初始化器从 JSON 创建模型类

ios - 包裹的 CATextLayer 中的字符被部分截断

ios - UIView背景图片不显示