简体   繁体   中英

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

i am trying to get user's location using only GPS provider. i don't want my app to get the last known location or request for location updates after a particular period of time or when location is changed.is there any way that can give me current location on button press??? ... i am a beginner, i have tried LocationManager API from android studio and it returns network provided location fine but dosent work when using GPS. here is my code:

 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()
            }
        }
    }

i have added location listener in the parent class " class AddCustomer: AppCompatActivity(), LocationListener "

in override function:

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

should i use "getcurrentlocation()" method from "LocationManager"?

I don't know if you are getting your last location or not. Here's the answer when you are not getting anything at all from the gps provider.

Making this block like this will only make you get your location via gps.

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

}

The above can return when your device's GPS chipset feels like it's getting signal from the GPS, so when inside a building, it will likely be not getting any responds. Go outside and try it out.

In your usecase, pressing a button to get an accurate location, in case the user can't receive any signal, you should provide a callback for timeout.

 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()
        }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM