I am creating an app which shows the location of the users using that app so, I tried setting the location value in the realtime database using firebase but Firebase doesn't set the value at all and there is no error in the logcat either.
here's the code:
private fun getLastLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
givePermission()
}
else{
fusedLocationProviderClient.lastLocation
.addOnCompleteListener(this) { task ->
//reference to the database
val database= FirebaseDatabase.getInstance()
val ref: DatabaseReference = database.getReference("test")
if (task.isSuccessful && task.result != null) {
val mLastLocation = task.result
//location details must be stored in the realtime db
ref.setValue(mLastLocation)
var address = "No known address"
val gcd = Geocoder(this, Locale.getDefault())
val addresses: List<Address>
try {
addresses = gcd.getFromLocation(mLastLocation!!.latitude, mLastLocation.longitude, 1)
if (addresses.isNotEmpty()) {
address = addresses[0].getAddressLine(0)
}
} catch (e: IOException) {
e.printStackTrace()
}
val height = 100
val width = 100
val bitmapdraw: BitmapDrawable = resources.getDrawable(R.drawable.ic_pickup) as BitmapDrawable
val b: Bitmap = bitmapdraw.bitmap
val smallMarker: Bitmap = Bitmap.createScaledBitmap(b, width, height, false)
googleMap.addMarker(
MarkerOptions()
.position(LatLng(mLastLocation!!.latitude, mLastLocation.longitude))
.title("Current Location")
.snippet(address)
)
val cameraPosition = CameraPosition.Builder()
.target(LatLng(mLastLocation.latitude, mLastLocation.longitude))
.zoom(25f)
.build()
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition))
}else {
Toast.makeText(this, "No current location found. Please check your internet connection", Toast.LENGTH_LONG).show()
}
}
}
}
I want to store the location details of the user in the realtime database but it doesn't seem to work for some reason. Please help. Thank you