gpt4 book ai didi

ios - 来自 locationManager didUpdateLocations 的 CLLocationCoordinates

转载 作者:行者123 更新时间:2023-11-28 13:14:15 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com