5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

kotlinでandroid入門 GPS

Posted at

GPS : LocationManager = ロケーション
スマホにあるGPSで現在位置を取得する方法です

1. パーミション

GPSを有効にするにはパーミッションが必要です
後は、外部ストレージのパーミッションを有効にした方法と同じで

有効にするパーミッションは
・ACCESS_COARSE_LOCATION
・ACCESS_FINE_LOCATION
単純にGPSの使用と、ネットワークから位置を取得することの許可

2. 位置情報取得

   override fun onCreate(savedInstanceState: Bundle?)  {
       // ・・・
       
       // 位置情報使用時に準備
       if ((PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION  )) and
           (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)))
       {
            // 位置情報を管理している LocationManager のインスタンスを生成
            var locationManager: LocationManager? = getSystemService(LOCATION_SERVICE) as LocationManager
            var locationProvider : String = ""
            
            if (null !== locationManager) {
                // GPSが利用可能になっているかどうかをチェック
                if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                    locationProvider = LocationManager.GPS_PROVIDER
                } else if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
                    locationProvider = LocationManager.NETWORK_PROVIDER
                } else {
                    // いずれも利用可能でない場合は、GPSを設定する画面に
                    val settingsIntent = Intent(ACTION_LOCATION_SOURCE_SETTINGS)
                    startActivity(settingsIntent)
                    return
                }

                // 変化検出時に呼ばれる
                locationManager.requestLocationUpdates(
                    locationProvider,
                    500, // 通知のための最小時間間隔(ミリ秒)
                    1.0F, // 通知のための最小距離間隔(メートル)
                    object : LocationListener {
                        override fun onLocationChanged(location: Location) {
                            // ここで座標をもとめている
                            val lat = location.latitude
                            val lng = location.longitude
                            LatitudeView.text = R.id.LongtudeView.toString() +" : " + lat.toString()
                            LongtudeView.text = R.id.LongtudeView.toString() +" : " + lng.toString()
                            Log.d("GPS", lat.toString() + "-" + lng.toString())
                        }
                        override fun onProviderDisabled(provider: String) {}
                        override fun onProviderEnabled(provider: String) {}
                        override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {}
                    }
                )
            }
       }
   }

Log.d()で位置を表示してます
エミュレータでのデバックなので、位置情報はかえれるのでそれを使ってデバック

やったことないけど、これ使うと何とかGOで世界中いけるのでは

5
5
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
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?