获取当前位置Android Kotlin [英] Get current location Android Kotlin

查看:344
本文介绍了获取当前位置Android Kotlin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to get the current Location with GM API in my application (using Android Studio). But if i click the button which triggers the getLocation() funktion, i always end up in the catch{} block and i dont know why. My mobile device is connected for testing.

这是getLocation()功能:

Here is the getLocation() Funktion:

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) {
        Toast.makeText(applicationContext, "Fehler bei der Erfassung!", Toast.LENGTH_SHORT).show()
    }
}

这是onCreate功能:

Here is the onCreate Funktion:

class CurrentLocationActivity : AppCompatActivity() {


lateinit var mapFragment : SupportMapFragment
lateinit var googleMap : GoogleMap

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_current_location)

    //Karte erstellen
    mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
    mapFragment.getMapAsync(OnMapReadyCallback {
        googleMap = it
    })



    //OnClickListener um Location zu speichern
    btnGetCurrentLocation.setOnClickListener {
        getLocation()
    }
}

推荐答案

尝试以下操作

第1步.放入您的AndroidManifest.xml

Step 1. Put on your AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com....">

    <!-- This line -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application ... />

</manifest>

第2步.将其放在您的位置请求上方

Step 2. Put it above your location request

import android.Manifest
import android.content.pm.PackageManager
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat

...

fun getLocation() {

    ...

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
                PERMISSION_REQUEST_ACCESS_FINE_LOCATION)
        return
    }
    locationManager!!.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0L, 0f, locationListener)
}

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    if (requestCode == PERMISSION_REQUEST_ACCESS_FINE_LOCATION) {
        when (grantResults[0]) {
            PackageManager.PERMISSION_GRANTED -> getLocation()
            PackageManager.PERMISSION_DENIED -> //Tell to user the need of grant permission
        }
    }
}

companion object {
    private const val PERMISSION_REQUEST_ACCESS_FINE_LOCATION = 100
}

这篇关于获取当前位置Android Kotlin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆