位置情報サービスを使用するとdidUpdateLocationsが呼び出され、現在地を取得することができます。非同期での呼び出しでかつ何度もロードされるので、後続処理を呼び出すときなどに厄介なことになりがちです。そういうときはdidUpdateLocationsのメソッド内で、位置情報取得完了フラグみたいなグローバル変数を用意して読み込み状況を判定してあげると処理をコントロールできるので便利です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
var currentLatitude: CLLocationDegrees! var currentLongitude: CLLocationDegrees! func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { if self.currentLongitude == nil && self.currentLatitude == nil { let newlocation = locations.first self.currentLongitude = newlocation?.coordinate.longitude self.currentLatitude = newlocation?.coordinate.latitude // next procees self.next() } } |