Home > Enterprise >  I am trying to find the location using FusedLocation Library
I am trying to find the location using FusedLocation Library

Time:08-13

I am trying to find the location using fused location library. However when I try to set text longitude and latitude in my textView, i keep on getting error. Please look into the code.

Code

private fun fetchLocation() {
    val task = fusedLocationProviderClient.lastLocation
    if(ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED)
    {
        ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION), 101)
        return
    }

    task.addOnSuccessListener {
        if(it!=null){
            val longitude = findViewById<TextView>(R.id.longitude)
            val latitude = findViewById<TextView>(R.id.latitude)

            longitude.setText(it.longitude)
            latitude.setText(it.latitude)
        }
    }
}

Error

<html>None of the following functions can be called with the arguments supplied:<br/>public final fun setText(text: CharSequence!): Unit defined in android.widget.TextView<br/>public final fun setText(resid: Int): Unit defined in android.widget.TextView

CodePudding user response:

Because the type of longitude/latitude is Double. And the type of the parameter of setText is either Int(resId) or CharSequence.

You could convert Double to String which implements CharSequence.

longitude.setText(it.longitude.toString())
latitude.setText(it.latitude.toString())
  • Related