I am trying to display current location in my app but it doesnt work . Problem here is that it doesnt asign new value to currentLocation .
It display error that current Location is not initialized which I understand so i used to initalize it on in create by Location("") which assigned to 0,0 but i still cannot assign to current place.
Here code :
private lateinit var mMap: GoogleMap
private lateinit var currentLocation : Location
private lateinit var fusedLocationProviderClient: FusedLocationProviderClient
private val REQUEST_CODE=101
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
fusedLocationProviderClient= LocationServices.getFusedLocationProviderClient(applicationContext as Context)
fetchLastLocation()
map_location_btn.setOnClickListener {
onCartClicked()
}
}
private fun fetchLastLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION), REQUEST_CODE)
return
}
var task : Task <Location> = fusedLocationProviderClient.lastLocation
task.addOnSuccessListener(object : OnSuccessListener<Location> {
override fun onSuccess(location: Location?) {
if(location !=null){
currentLocation =location
Toast.makeText(applicationContext, "Lati: "+currentLocation.latitude.toString() + " Long: " +
currentLocation.longitude.toString(), Toast.LENGTH_SHORT).show()
val supportMapFragment = (supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?)!!
supportMapFragment.getMapAsync(this@MapsActivity)
}
}
})
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
// Add a marker in Sydney and move the camera
val latLng = LatLng(currentLocation.latitude, currentLocation.longitude)
val markerOptions = MarkerOptions().position(latLng).title("I am here")
googleMap?.animateCamera(CameraUpdateFactory.newLatLng(latLng))
googleMap?.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 5f))
mMap.addMarker(markerOptions)
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String?>,
grantResults: IntArray) {
if(requestCode == REQUEST_CODE){
if (grantResults.isNotEmpty() && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
fetchLastLocation()
}
}
}