I recently added get location function. When I try to show longitude and latitude, it returns zero.
This my LocationListener class:
inner class MylocationListener: LocationListener { constructor():super(){ mylocation= Location("me") mylocation!!.longitude mylocation!!.latitude } override fun onLocationChanged(location: Location?) { mylocation=location } override fun onStatusChanged(p0: String?, p1: Int, p2: Bundle?) {} override fun onProviderEnabled(p0: String?) {} override fun onProviderDisabled(p0: String?) {} }
And this my GetUserLocation function:
fun GetUserLocation(){ var mylocation= MylocationListener() var locationManager=getSystemService(Context.LOCATION_SERVICE) as LocationManager locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0.1f,mylocation) }
And this my function to return my longitude and latitude:
fun getLoction (view: View){ prgDialog!!.show(); GetUserLocation() button.setTextColor(getResources().getColor(R.color.green)); textView.text = mylocation!!.latitude.toFloat().toString() Toast.makeText(this, mylocation!!.latitude.toFloat().toString(), Toast.LENGTH_LONG).show() Toast.makeText(this, mylocation!!.longitude.toFloat().toString(), Toast.LENGTH_LONG).show() prgDialog!!.hide() }
This example demonstrates how to get the current GPS location programmatically on Android using Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
There are two ways to get the current location of any Android device: Android's Location Manager API. Fused Location Provider: Google Play Services Location APIs.
On Kotlin, you access the location data using the lastLocation provided by FusedLocationProviderClient and it will return the location data such as latitude, longitude, provider, etc. using the fused location provider requires you to grant the location permission.
In 2019 Best Offical Solution in Kotlin
Google API Client/FusedLocationApi are deprecated and Location Manager is not useful at all. So Google prefer Fused Location Provider Using the Google Play services location APIs "FusedLocationProviderClient" is used to get location and its better way for battery saving and accuracy
Here is sample code in kotlin to get the last known location /one-time location( equivalent to the current location)
// declare a global variable of FusedLocationProviderClient private lateinit var fusedLocationClient: FusedLocationProviderClient // in onCreate() initialize FusedLocationProviderClient fusedLocationClient = LocationServices.getFusedLocationProviderClient(context!!) /** * call this method for receive location * get location and give callback when successfully retrieve * function itself check location permission before access related methods * */ fun getLastKnownLocation() { fusedLocationClient.lastLocation .addOnSuccessListener { location-> if (location != null) { // use your location object // get latitude , longitude and other info from this } } }
If your app can continuously track the location then you have to receive Receive location updates
Check the sample for that in kotlin
// declare a global variable FusedLocationProviderClient private lateinit var fusedLocationClient: FusedLocationProviderClient // in onCreate() initialize FusedLocationProviderClient fusedLocationClient = LocationServices.getFusedLocationProviderClient(context!!) // globally declare LocationRequest private lateinit var locationRequest: LocationRequest // globally declare LocationCallback private lateinit var locationCallback: LocationCallback /** * call this method in onCreate * onLocationResult call when location is changed */ private fun getLocationUpdates() { fusedLocationClient = LocationServices.getFusedLocationProviderClient(context!!) locationRequest = LocationRequest() locationRequest.interval = 50000 locationRequest.fastestInterval = 50000 locationRequest.smallestDisplacement = 170f // 170 m = 0.1 mile locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY //set according to your app function locationCallback = object : LocationCallback() { override fun onLocationResult(locationResult: LocationResult?) { locationResult ?: return if (locationResult.locations.isNotEmpty()) { // get latest location val location = locationResult.lastLocation // use your location object // get latitude , longitude and other info from this } } } } //start location updates private fun startLocationUpdates() { fusedLocationClient.requestLocationUpdates( locationRequest, locationCallback, null /* Looper */ ) } // stop location updates private fun stopLocationUpdates() { fusedLocationClient.removeLocationUpdates(locationCallback) } // stop receiving location update when activity not visible/foreground override fun onPause() { super.onPause() stopLocationUpdates() } // start receiving location update when activity visible/foreground override fun onResume() { super.onResume() startLocationUpdates() }
Make sure you take care about Mainfaist permission and runtime permission for location
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
and for Gradle add this
implementation 'com.google.android.gms:play-services-location:17.0.0'
For more details follow these official documents
https://developer.android.com/training/location/retrieve-current
https://developer.android.com/training/location/receive-location-updates
https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient
When GetUserLocation
returns, locationManager
goes out of scope and presumably is destroyed, preventing onLocationChanged
from being called and providing updates.
Also, you've defined mylocation
inside of GetUserLocation
so it also goes out of scope and further kills any chance or your getting an update.
You have not shown where and how the outer mylocation
is declared (outside of GetUserLocation
), but how ever it is declared, it is being shadowed by the one inside of GetUserLocation
. So you aren't getting much.
Here is an example of how you might do it. (The variable thetext
is defined within the layout xml and accessed with Kotlin extensions.)
// in the android manifest <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> // allow these through Appliation Manager if necessary // inside a basic activity private var locationManager : LocationManager? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) // Create persistent LocationManager reference locationManager = getSystemService(LOCATION_SERVICE) as LocationManager? fab.setOnClickListener { view -> try { // Request location updates locationManager?.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0L, 0f, locationListener) } catch(ex: SecurityException) { Log.d("myTag", "Security Exception, no location available") } } } //define the listener private val locationListener: LocationListener = object : LocationListener { override fun onLocationChanged(location: Location) { thetext.text = ("" + location.longitude + ":" + location.latitude) } override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {} override fun onProviderEnabled(provider: String) {} override fun onProviderDisabled(provider: String) {} }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With