Getting this error when trying to get location from latlong using Geocoder.
Error
Caused by java.io.IOException: grpc failed at android.location.Geocoder.getFromLocation(Geocoder.java:136) at com.example.myApp.test.Fragments.DashboardFragment$sendLocationData$1.onSuccess(DashboardFragment.kt:676) at com.example.myApp.test.Fragments.DashboardFragment$sendLocationData$1.onSuccess(DashboardFragment.kt:81) at com.google.android.gms.tasks.zzj.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6776) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
Code
punch_button?.setOnClickListener {
createLocationRequest()
}
protected fun createLocationRequest() {
mLocationRequest = LocationRequest()
mLocationRequest?.interval = 10
mLocationRequest?.fastestInterval = 50
mLocationRequest?.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
val builder = LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest!!)
mLocationCallback = object : LocationCallback() {
override fun onLocationResult(p0: LocationResult?) {
super.onLocationResult(p0)
mCurrentLocation = p0?.lastLocation
}
}
val client = LocationServices.getSettingsClient(context)
val task = client.checkLocationSettings(builder.build())
task.addOnSuccessListener(OnSuccessListener<LocationSettingsResponse> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
askForPermission();
}
}) }
private fun askForPermission() {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.ACCESS_FINE_LOCATION)) {
requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 123);
} else {
requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 123);
}
} else {
sendLocationData()
}
}
@SuppressLint("MissingPermission")
fun sendLocationData() {
try{
Log.e("lat", mCurrentLocation?.latitude.toString())
//mCurrentLocation?.latitude !=null && mCurrentLocation?.latitude !=null
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null)
mFusedLocationClient.lastLocation.addOnSuccessListener({ location ->
if (location != null) {
val address: List<Address> = geocoder?.getFromLocation(location.latitude, location.longitude, 1)!!
}
})
}catch(e:Exception){
Log.e("tag","error")
}
}