0

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?

kndnba
  • 3
  • 2

2 Answers2

0

as It can take some time to update the position, you should register a callback for retrieving the current location. Note that this code only returns the last known location, if you want to get updated location continuously, you can use the requestLocationUpdates method.

fusedLocationClient.getLastLocation()
    .addOnSuccessListener(this, new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {
            // Got last known location. In some rare situations this can be null.
            if (location != null) {
                // Logic to handle location object
            }
        }
    });
bongo
  • 733
  • 4
  • 12
0
  1. Turn on your location. 2. check whether location is null or not:

    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
    
     if (location != null) { //<------ 
     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
     } else { //<------
             Toast.makeText(getActivity(), "Null location!",
                                           Toast.LENGTH_LONG).show();
     }
    

    }

If the above code returned else part, then most likely your last location is empty (if you are using emulator!) In emulator like BlueStacks Ctrl+Shift +K (for other emulators see this post: How to emulate GPS location in the Android Emulator?) and set a location, then try your code again.

C.F.G
  • 817
  • 8
  • 16