Hoegje
Hoegje

Reputation: 75

How to get the location information from my Kotlin code back to a flutter project?

Preface: Developing for WearOS, so I can't use the location or geo_location package for that in my flutter code.

So for WearOs: I want to do this in my flutter code:

const platform = MethodChannel(
    "myapp/location_permission");
await platform.invokeMethod('requestPermission');
final bool result = await platform.invokeMethod('checkPermission');
double? latitude;
double? longitude;
if (result) {
  latitude = await platform.invokeMethod("getLocationLatitude");
  longitude = await platform.invokeMethod("getLocationLongitude");
}

So I added some code to my MainActivity.kt as described here https://github.com/Baseflow/flutter-geolocator/issues/1197

But I don't want to start and stop the follow, but just a snapshot of the current location. So I tried this:

private fun getLocationLongitude(): Double {
    var location = 2.2;
    if (checkLocationPermission()) {
        val cancellationTokenSource = CancellationTokenSource()
        fusedLocationClient.getCurrentLocation(
            android.location.LocationRequest.QUALITY_HIGH_ACCURACY,
            cancellationTokenSource.getToken()
        ).addOnSuccessListener { returnedLocation ->
            location = returnedLocation.getLongitude()
        }
        return location
    }
    return location
}

But it always returns 2.2, I guess this is probably because it does not wait for the location to be fetched. how can I "await" this getcurrentLocation method call so that the Task is completed and I can get the actual result from it?

Upvotes: 0

Views: 50

Answers (0)

Related Questions