【问题标题】:How can i get current GPS location ? (not lastknown)如何获取当前 GPS 位置? (最后不知道)
【发布时间】:2021-05-08 10:15:08
【问题描述】:

我正在尝试仅使用 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, 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 AddCustomer: AppCompatActivity(), LocationListener”中添加了位置监听器

在覆盖函数中:

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

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

【问题讨论】:

    标签: android android-studio gps locationmanager currentlocation


    【解决方案1】:

    我不知道您是否获得了最后一个位置。当您从 gps 提供商处得不到任何东西时,这就是答案。

    像这样制作这个块只会让你通过 gps 获得你的位置。

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

    当您的设备的 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()
            }
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-11
      • 2016-07-30
      • 1970-01-01
      • 1970-01-01
      • 2016-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多