LoginSignup
0
2

More than 3 years have passed since last update.

Swift MkMapViewで地図アプリ作成してみた(17)- バックグラウンド・スリープ中でも位置情報を受信する

Posted at

記事一覧

Swift MkMapViewで地図アプリ作成してみた(記事一覧)

バックグラウンド・スリープ中でも位置情報を受信する

CLLocationManagerのプロパティを設定する

allowsBackgroundLocationUpdatesプロパティをtrueにすれば、
バックグラウンドでGPSを受信することができる。

プロパティを設定する
locManager = CLLocationManager()
locManager.allowsBackgroundLocationUpdates = true

バックグラウンドにした時のデバイスの画面

正しく受信できれば、左上に位置情報を受信しているアイコンが表示される。
スクリーンショット 2020-03-29 9.31.12.png

位置情報を受信する関数

バックグラウンド・スリープ中も、下記の関数で位置情報を受信することができる。
位置情報の取得方法は、(02)- 現在位置を取得するを参照してください。

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations:[CLLocation]) {
    let lonStr = (locations.last?.coordinate.longitude.description)!
    let latStr = (locations.last?.coordinate.latitude.description)!
}

参考文書

Apple Documentation > allowsbackgroundlocationupdates

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2