我怎样才能得到当前的 GPS 位置? (不为人所知)

How can i get current GPS location ? (not lastknown)

我正在尝试仅使用 GPS 提供商获取用户的位置。我不希望我的应用在特定时间段后或当位置为 changed.is 时获取最后已知位置或请求位置更新有什么方法可以在按下按钮时给我当前位置??? ...我是初学者,我尝试过 android studio 的 LocationManager API,它 returns 网络提供的位置很好,但在使用 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, 10000, 5F, 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 " class AddCustomer : AppCompatActivity() , LocationListener "

在覆盖函数中:

override fun onLocationChanged(location: Location) {
       locationx.text = location.latitude.toString() +" , "+ location.longitude.toString()
    }

我应该使用“LocationManager”中的“getcurrturlocation()”方法吗?

我不知道你是否得到了你最后的位置。当您从 gps 提供商那里什么都得不到时,这里是答案。

像这样制作此块只会让您通过 gps 获取您的位置。

if (gps) {
                
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 5F, this)

}

以上可以 return 当您设备的 GPS 芯片组感觉它正在从 GPS 获取信号时,因此在建筑物内时,它可能不会收到任何响应。 到外面去试试。

在你的用例中,按下一个按钮来获得准确的位置,如果用户收不到任何信号,你应该提供一个超时回调。

 private val mHandler = Handler(Looper.getMainLooper())

 private val mTimeoutRunnable = Runnable {
    
     //stopLoading
     //Toast the user to try it somewhere else
 }


 fun askGsp() {

        mHandler.postDelayed(mTimeoutRunnable, 5 * 1000) //time out in 5 seconds
        
        //ask for gps update like the code above
        //...
        override fun onLocationChanged(location: Location) {

              mHandler.removeCallbacks(mTimeoutRunnable) //remove timeout action
               locationx.text = location.latitude.toString() +" , "+ 
               location.longitude.toString()
        }
 }