问题描述
我正在尝试仅使用 GPS 提供商获取用户的位置。我不希望我的应用程序在特定时间段或位置更改后获取最后一个已知位置或请求位置更新。有什么方法可以在按下按钮时为我提供当前位置??? ...我是一个初学者,我尝试过 android studio 的 LocationManager API,它返回网络提供的位置很好,但在使用 GPS 时不工作。这是我的代码:
fun getlocation() {
if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,arrayOf(Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION),locationPermissionCode)
}
else
{
locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
// val listener = locationListener()
val gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
val network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
//if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N)
if (gps || network) {
if (network) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,10000,5F,this)
}
if (gps) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,this)
}
}
else {
val AlertDialog = AlertDialog.Builder(this)
AlertDialog.setTitle("GPS is Disabled")
AlertDialog.setMessage("Do you want enable GPS ?")
AlertDialog.setPositiveButton("Yes") { dialougeInterface: DialogInterface,i: Int ->
val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
startActivity(intent)
dialougeInterface.dismiss()
}
AlertDialog.setNegativeButton("No") { dialouge: DialogInterface,i: Int ->
dialouge.dismiss()
}
AlertDialog.show()
}
}
}
我在父类“ class AddCustomer : AppCompatActivity(),LocationListener ”中添加了位置侦听器
在覆盖功能中:
override fun onLocationChanged(location: Location) {
locationx.text = location.latitude.toString() +","+ location.longitude.toString()
}
我应该使用“LocationManager”中的“getcurrentlocation()”方法吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)