User0829
User0829

Reputation: 187

getCurrentLocation() method in Kotlin?

While I am trying to implement a simple example getting the location of the device, I found that a document which is "seemingly official": https://developer.android.com/training/location/retrieve-current#BestEstimate

The document claims that FusedLocationProviderClient provides the following two methods: getLastLocation() and getCurrentLocation(). But as one can see in the example - https://developer.android.com/training/location/retrieve-current#last-known - both getLast/CurrentLocation() lives in Java. The corresponding Kotlin example says that fusedLocationClient.getLastLocation() "is the same as" fusedLocationClient.lastLocation and, indeed, it works well.

I naively assume that there should be corresponding "currentLocation" for example, fusedLocationClient.currentLocation.

I am wondering there is no such, or I am the only one who fails to find the corresponding Kotlin method.

Upvotes: 4

Views: 8183

Answers (2)

Shivanand Darur
Shivanand Darur

Reputation: 3224

fusedLocationClient.getCurrentLocation(Priority.PRIORITY_HIGH_ACCURACY, object : CancellationToken() {
        override fun onCanceledRequested(listener: OnTokenCanceledListener) = CancellationTokenSource().token

        override fun isCancellationRequested() = false
    })
    .addOnSuccessListener {
        if (it == null)
            Toast.makeText(this, "Cannot get location.", Toast.LENGTH_SHORT).show()
        else {
            val lat = it.latitude
            val lon = it.longitude
        }

    }

Upvotes: 1

Ivo
Ivo

Reputation: 23144

in kotlin any method of the form getX can be written as just x, this is called "property access syntax". There is no separate kotlin version. fusedLocationClient.lastLocation is really exactly the same as fusedLocationClient.getLastLocation(). You can even write this last form in kotlin if you want.

However, this is only true for "get" methods without parameters. The thing is, getCurrentLocation does have parameters so property access syntax is not possible in this case. as you can see here this is the signature of this method:

public Task<Location> getCurrentLocation (int priority, CancellationToken token)

So you should use it like that. for example

fusedLocationClient.getCurrentLocation(LocationRequest.PRIORITY_HIGH_ACCURACY, null)

EDIT:

apparently null as parameter is not allowed. According to https://stackoverflow.com/a/72159436/1514861 this is a possibility:

fusedLocationClient.getCurrentLocation(LocationRequest.PRIORITY_HIGH_ACCURACY, object : CancellationToken() {
            override fun onCanceledRequested(p0: OnTokenCanceledListener) = CancellationTokenSource().token

            override fun isCancellationRequested() = false
        })
        .addOnSuccessListener { location: Location? ->
            if (location == null)
                Toast.makeText(this, "Cannot get location.", Toast.LENGTH_SHORT).show()
            else {
                val lat = location.latitude
                val lon = location.longitude
            }

        }

Upvotes: 6

Related Questions