位置情報を使って簡単なiOSアプリを作りたいと思っています。残念ながら、デバイスとシミュレータの両方でテストしている間、シミュレータ上の "現在地"デバッグコードを入力しても、私は 'nil'を受け取ります。 Swift 3でCoreLocationを初めて使用したので、前回と同じ方法を使用しました。位置情報の問題3
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var latitudeLabel: UILabel!
@IBOutlet weak var longitudeLabel: UILabel!
var locationManager:CLLocationManager?
var currentLocation:CLLocation?
override func viewDidLoad() {
super.viewDidLoad()
let locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
updateLabels()
// Do any additional setup after loading the view, typically from a nib.
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
self.currentLocation = locations[0]
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error)
}
func updateLabels() {
latitudeLabel.text = String(describing: currentLocation?.coordinate.latitude)
longitudeLabel.text = String(describing: currentLocation?.coordinate.longitude)
print(currentLocation)
}
}
もちろん、私はInfo.plistに必要なプライバシーキーをすべて書きました。
私はcurrentLocationを印刷しようとしていますが、私はnilを受け取ります。 アラートが表示されていますが、すぐに消えてしまうような問題が見つかりました。
あなたは「私は 『ゼロ』を受ける」とはどういう意味ですか?問題を明確にしてください。応答のために – rmaddy
ありがとうございます。編集された説明。 –