I recently added get location function. When I try to show longitude and latitude, it returns zero.
This my LocationListener class:
inner class Myloc
I read many of answers but question is get only last known location. With receiver it continuously send latitude and longitude I have solution for this in kotlin.. Give permissions
public fun getLastKnownLocation(context: Context) {
val locationManager: LocationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
val providers: List = locationManager.getProviders(true)
var location: Location? = null
for (i in providers.size - 1 downTo 0) {
location= locationManager.getLastKnownLocation(providers[i])
if (location != null)
break
}
val gps = DoubleArray(2)
if (location != null) {
gps[0] = location.getLatitude()
gps[1] = location.getLongitude()
Log.e("gpsLat",gps[0].toString())
Log.e("gpsLong",gps[1].toString())
}
}