I do not know how to get latitude and longitude using GoogleApiClient

0

I'm developing an android app using Kotlin, I followed a tutorial on a topic from here, however it took the last location that was saved, not latitude and longitude, which is what I need. Here is the code:

class InserirAbastecimentoActivity : AppCompatActivity(), GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

lateinit var googleApiClient:GoogleApiClient

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_inserir_abastecimento)
    setSupportActionBar(toolbar)

    googleApiClient = GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build()

    //Conectar com GoogleApi
    googleApiClient.connect()
...}
    override fun onStop() {
    super.onStop()
    pararConexaoComGoogleApi()
}

fun pararConexaoComGoogleApi(){
    if (googleApiClient.isConnected){
        googleApiClient.disconnect()
    }
}

override fun onConnected(p0: Bundle?) {
    Log.d("Tag", "Conexão bem sucedida")
}

override fun onConnectionSuspended(p0: Int) {
}

override fun onConnectionFailed(p0: ConnectionResult) {
    pararConexaoComGoogleApi()
}

}

    
asked by anonymous 08.05.2018 / 15:56

1 answer

0

I would recommend using FusedLocationProviderClient.

fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)

You add a listener in it that will return you a Location object when it can get the location and within it are the latitude of logitude:

fusedLocationClient.lastLocation .addOnSuccessListener { location : Location? -> var latitude= location.latitude.toString() var longitude= location.longitude.toString() }

Source: link

    
08.08.2018 / 17:23