获取当前位置的详细信息的AndroidKotlin代码?

zi8p0yeb  于 2023-01-26  发布在  Kotlin
关注(0)|答案(1)|浏览(146)

请为我提供使用FusedLocationProviderClient获取当前位置的androidKotlin示例代码?

zpgglvta

zpgglvta1#

使用以下代码..

private lateinit var fusedLocationClient: FusedLocationProviderClient

override fun onCreate(savedInstanceState: Bundle?) {
    // ...

    fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
    fusedLocationClient.lastLocation
        .addOnSuccessListener { location : Location? ->
            val latitude = location.latitude
            val longitude = location.longitude
        }
}
备注

您还必须在Manifest.xml文件中声明位置权限

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

依赖性:

implementation "com.google.android.gms:play-services-location:21.0.1"

相关问题