[英]Android Kotlin update position
我正在處理我的第一個 Android 項目。 我可以拉出最后一個 GPS 位置,但是當我移動 position 時,它不會更新。 如果我切換到地圖應用程序然后回到我的應用程序,GPS 位置將更新,所以這告訴我我需要以某種方式查詢系統以更新當前位置,但無法弄清楚如何做到這一點。
我錯過了什么?
下面是我目前正在使用的。
class MainActivity : AppCompatActivity() {
private lateinit var fusedLocationProviderClient: FusedLocationProviderClient
private lateinit var tvLatitude:TextView
private lateinit var tvLongitude:TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
tvLatitude=findViewById(R.id.tvLat)
tvLongitude=findViewById(R.id.tvLon)
val cmdUpdateLocation = findViewById<Button>(R.id.cmdUpdateLocation)
cmdUpdateLocation?.setOnClickListener()
{
getCurrentLocation()
}
getCurrentLocation()
}
private fun getCurrentLocation() {
if(checkPermissions())
{
if(isLocationEnabled())
{
//FInal lat and lon
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
requestPermission()
return
}
fusedLocationProviderClient.lastLocation.addOnCompleteListener(this){ task->
val location: Location?=task.result
if(location==null)
{
Toast.makeText(applicationContext,"Nul received",Toast.LENGTH_SHORT).show()
}
else
{
Toast.makeText(applicationContext,"Get Success",Toast.LENGTH_SHORT).show()
tvLatitude.text=""+location.latitude
tvLongitude.text=""+location.longitude
}
}
}
else
{
//Settings Open
Toast.makeText(applicationContext,"Turn on Location",Toast.LENGTH_SHORT).show()
val intent=Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
startActivity(intent)
}
}
else
{
//Request Permission here
requestPermission()
}
}
private fun isLocationEnabled():Boolean{
var locationManager:LocationManager=getSystemService(Context.LOCATION_SERVICE) as LocationManager
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)||locationManager.isProviderEnabled(
LocationManager.GPS_PROVIDER
)
}
private fun requestPermission() {
ActivityCompat.requestPermissions(
this,arrayOf(android.Manifest.permission.ACCESS_COARSE_LOCATION,
android.Manifest.permission.ACCESS_FINE_LOCATION),
PERMISSION_REQUEST_ACCESS_LOCATION
)
}
companion object{
private const val PERMISSION_REQUEST_ACCESS_LOCATION=100
}
private fun checkPermissions():Boolean
{
if(ActivityCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_COARSE_LOCATION)
==PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
{
return true
}
return false
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if(requestCode== PERMISSION_REQUEST_ACCESS_LOCATION)
{
if(grantResults.isNotEmpty() && grantResults[0]==PackageManager.PERMISSION_GRANTED)
{
Toast.makeText(applicationContext,"Granted",Toast.LENGTH_SHORT).show()
getCurrentLocation()
}
else
{
Toast.makeText(applicationContext,"Denied",Toast.LENGTH_SHORT).show()
}
}
}
}
您需要調用 requestLocationUpdates,而不是 lastLocation。 lastLocation 僅返回最后一個已知位置一次,並且經常返回 null(如果位置未打開)。 它不會打開位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.