How to ask users location everytime the app is opened

33 views Asked by At

Im making a kotlin application that displays roadwork going on near users. Im currently trying to pin point the users location via pop up and display it on the map. When I first implemented this method I received the pop up but since then it doesnt pop up and the users location is accepted (I think).

private fun fetchLocation() {
        val task = fusedLocationProviderClient.lastLocation

        if (checkSelfPermission(requireContext(), android.Manifest.permission.ACCESS_FINE_LOCATION)

            != PackageManager.PERMISSION_GRANTED && ActivityCompat
                .checkSelfPermission(
                    requireContext(),
                    android.Manifest.permission.ACCESS_COARSE_LOCATION
                ) != PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(
                requireActivity(),
                arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION),
                101
            )
            return
        }
        task.addOnSuccessListener {
            if (it != null) {
                Toast.makeText(
                    requireContext(),
                    "your location has been added to the map",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }

    }
0

There are 0 answers