gpt4 book ai didi

ios - 快速获取用户位置

转载 作者:可可西里 更新时间:2023-11-01 01:06:43 26 4
gpt4 key购买 nike

这是我的代码:

import Foundation
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

let locationManager = CLLocationManager()

@IBOutlet weak var locationLabel: UILabel!

var coord: CLLocationCoordinate2D?

override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized {
locationManager.startUpdatingLocation()
println(coord!.longitude)
locationLabel.text = "location found"
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
var locationArray = locations as NSArray
var locationObj = locationArray.lastObject as CLLocation
coord = locationObj.coordinate

}


}
}

此代码不返回任何内容(它应该打印经度)。这似乎没问题,因为如果我移动

            locationManager.startUpdatingLocation()
println(coord!.longitude)

在 if 语句之外,Xcode 抛出一个运行时错误,并表示它在解包 Optional 值时意外发现 nil。我不明白为什么?系统要求我允许使用我的位置。

谢谢!

最佳答案

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}

func locationManager(manager:CLLocationManager, didUpdateLocations locations:AnyObject[] {
println("locations = \(locations)")
gpsResult.text = "success"
}

关于ios - 快速获取用户位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26109669/

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