Olá, tudo bom brother?
Como que eu faço para enviar os dados da localização atual para outra activity? Eu consigo pegar os dados da localização atual, mas não consigo enviar para outra activity. Poderia me ajudar por favor?
class MainActivity : AppCompatActivity(), OnMapReadyCallback,
GoogleMap.OnMarkerClickListener {
override fun onMarkerClick(p0: Marker?) = false
private lateinit var map: GoogleMap
private lateinit var lastLocation: Location
private lateinit var fusedLocationClient: FusedLocationProviderClient
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val formatDate = SimpleDateFormat("dd/M/yyyy hh:mm:ss")
val dataInicial = formatDate.format(Date())
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
btIniciarVisita.setOnClickListener {
val situacaoIntent = Intent(this@MainActivity, SituacaoActivity::class.java)
// var dadosMapa = currentLatLng.toString()
// intent.putExtra("DadosMapa", dadosMapa)
intent.putExtra("DataInicial", dataInicial)
Log.i("TAG",dataInicial)
startActivity(situacaoIntent)
}
}
override fun onMapReady(googleMap: GoogleMap) {
map = googleMap
map.getUiSettings().setZoomControlsEnabled(true)
map.setOnMarkerClickListener(this)
if (ActivityCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION), LOCATION_PERMISSION_REQUEST_CODE)
return
}
map.isMyLocationEnabled = true
fusedLocationClient.lastLocation.addOnSuccessListener(this) { location ->
// Got last known location. In some rare situations this can be null.
if (location != null) {
lastLocation = location
val currentLatLng = LatLng(location.latitude, location.longitude)
placeMarkerOnMap(currentLatLng)
map.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, 12.0f))
}
}
}
companion object {
private const val LOCATION_PERMISSION_REQUEST_CODE = 1
}
private fun placeMarkerOnMap(location: LatLng) {
val markerOptions = MarkerOptions().position(location)
map.addMarker(markerOptions)
}