0

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

3 回答 3

1

你应该搬进var pickupLocation = coordinate!你的didUpdateLocations. 一旦分配,您也locationManager.stopUpdatingLocation()可以从内部'didUpdateLocation或您的值调用以保持更新。停止 locationManager 后,调用一个 NEW 函数来运行您当前的其余代码func uberButtonPressed

于 2015-04-22T21:22:50.603 回答
1

我有同样的问题。

而不是像这样调用委托方法:

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    debugPrint("NOT CALLED-- didUpdateLocations AnyObject")    
}

我变成了:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    debugPrint("CALLED-- didUpdateLocations CLLocation")
}
于 2016-12-28T16:16:26.437 回答
0

问题是您正在解包coordinateaCLLocationCoordinate2D?和 a !。由于坐标来自位置,即 a CLLocation?。坐标和位置可能为零,尤其是在定位服务有机会启动之前。仅uberButtonPressed:在坐标和位置不为零的情况下使用 运行其余部分if let currentCoordinate = coordinate?,在这种情况下,调用locationManager.stopUpdatingLocation()

于 2015-04-22T21:01:30.417 回答