Home > front end >  getCurrentLocation() method in Kotlin?
getCurrentLocation() method in Kotlin?

Time:02-16

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.

CodePudding user response:

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)
  • Related