I'm working on my study android project where I need to place 5 random markers within a 10km radius of my geolocation. Here's my function to generate random coordinates:
     fun generateRandomCoordinates(min: Int, max: Int): LatLng {
        val coordinates: LatLng
        val currentLong: Double
        val currentLat: Double
        val meterCord = 0.00900900900901 / 1000
        //Generate random Meters between the maximum and minimum Meters
        val r = Random()
        val randomMeters: Int = r.nextInt(max + min)
        //then Generating Random numbers for different Methods
        val randomPM: Int = r.nextInt(6)
        //Then we convert the distance in meters to coordinates by Multiplying number of meters with 1 Meter Coordinate
        val metersCordN = meterCord * randomMeters.toDouble()
        val locationResult = fusedLocationProviderClient.lastLocation
        currentLong = locationResult.result.longitude
        currentLat= locationResult.result.latitude
        coordinates = when (randomPM) {
            0 -> LatLng(currentLat + metersCordN, currentLong + metersCordN)
            1 -> LatLng(currentLat - metersCordN, currentLong - metersCordN)
            2 -> LatLng(currentLat + metersCordN, currentLong - metersCordN)
            3 -> LatLng(currentLat - metersCordN, currentLong + metersCordN)
            4 -> LatLng(currentLat, currentLong - metersCordN)
            else -> LatLng(currentLat - metersCordN, currentLong)
        }
        return coordinates
    }
that code results with java.lang.IllegalStateException: Task is not yet complete at line currentLong = locationResult.result.longitude
I tried to use addOnCompleteListener but somehow it didn't worked out. So I tried to add Thread.sleep(50) and that works but I think that it's not how sane person should code. How can I solve this problem?
 
     
    