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()
}
}