小编G. *_*kas的帖子

获取当前位置 Android Kotlin

我尝试在我的应用程序中使用 GM API 获取当前位置(使用 Android Studio)。但是,如果我单击触发 getLocation() 函数的按钮,我总是会进入 catch{} 块,但我不知道为什么。我的移动设备已连接以进行测试。

这是 getLocation() 函数:

fun getLocation() {

    var locationManager = getSystemService(LOCATION_SERVICE) as LocationManager?

    var locationListener = object : LocationListener{
        override fun onLocationChanged(location: Location?) {
            var latitute = location!!.latitude
            var longitute = location!!.longitude

            Log.i("test", "Latitute: $latitute ; Longitute: $longitute")

        }

        override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {
        }

        override fun onProviderEnabled(provider: String?) {
        }

        override fun onProviderDisabled(provider: String?) {
        }

    }

    try {
        locationManager!!.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0L, 0f, locationListener)
    } catch (ex:SecurityException) …
Run Code Online (Sandbox Code Playgroud)

maps android google-maps currentlocation kotlin

3
推荐指数
1
解决办法
3万
查看次数

标签 统计

android ×1

currentlocation ×1

google-maps ×1

kotlin ×1

maps ×1