Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading