|
|
|
|
@ -44,6 +44,8 @@ class LocationManager(private var context: Context) { |
|
|
|
|
// Call findCurrentPlace and handle the response (first check that the user has granted permission). |
|
|
|
|
if (ContextCompat.checkSelfPermission(context, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { |
|
|
|
|
val placeResponse = placesClient.findCurrentPlace(request) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
placeResponse.addOnCompleteListener { task -> |
|
|
|
|
val places = ArrayList<PlaceLikelihood>() |
|
|
|
|
if (task.isSuccessful) { |
|
|
|
|
@ -142,7 +144,31 @@ class LocationManager(private var context: Context) { |
|
|
|
|
// If we don't have the permission, return null |
|
|
|
|
callback?.invoke(null) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Return the current location of the user |
|
|
|
|
*/ |
|
|
|
|
fun findCurrentLocation(callback: ((location: android.location.Location?) -> Unit)?) { |
|
|
|
|
val fusedLocationClient: FusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(context) |
|
|
|
|
if (ContextCompat.checkSelfPermission(context, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { |
|
|
|
|
fusedLocationClient.lastLocation.addOnSuccessListener { location: android.location.Location? -> |
|
|
|
|
// Got last known location. In some rare situations this can be null. |
|
|
|
|
location?.let { currentLocation -> |
|
|
|
|
callback?.invoke(currentLocation) |
|
|
|
|
} ?: run { |
|
|
|
|
// If the current location is null, return null |
|
|
|
|
callback?.invoke(null) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
}.addOnCanceledListener { |
|
|
|
|
// If there was a problem during the call to last location, return null |
|
|
|
|
callback?.invoke(null) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
callback?.invoke(null) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |