【问题标题】:CLLocationCoordinates from locationManager didUpdateLocations来自 locationManager 的 CLLocationCoordinates didUpdateLocations
【发布时间】:2015-04-22 20:16:42
【问题描述】:

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

【问题讨论】:

    标签: ios swift cllocationmanager cllocationcoordinate2d uber-api


    【解决方案1】:

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

    【讨论】:

    • 您好,谢谢输入...我尝试将pickupLocation移动到didUpdateLocations,这很有效,我得到了坐标并正确实施了stopUpdatingLocation()。我对调用一个 NEW 函数来运行 func uberButtonPressed 中的其余代码的意思有点困惑......你的意思是我应该取出代码(来自 uberButtonPressed) - 从 var uber = Uber(pickupLocation :pickLocation!),创建一个单独的函数,然后从 didUpdateLocations 调用它?我已经尝试过了,pickupLocation 仍然为零...问题可能是 uberButtonPressed 运行第一个?
    • 是的,没错,您应该在停止位置更新后调用一个新函数。新函数将包含从 var uber = Uber(pickupLocation: pickupLocation!) 开始的代码。您可以将 pickupLocation 传递给新函数,也可以将 pickupLocation 设为类的属性,以便在新函数被赋值后从新函数中读取它didUpdateLocation
    【解决方案2】:

    我遇到了同样的问题。

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

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

    我变成了:

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        debugPrint("CALLED-- didUpdateLocations CLLocation")
    }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-01
        • 2013-11-06
        相关资源
        最近更新 更多