I've solved. Here is the code.

locationManager.getCurrentLocation(
    LocationManager.GPS_PROVIDER,
    null,
    application.getMainExecutor(),
    new Consumer<Location>() {
  @Override
  public void accept(Location location) {
    // code
  }
});
Answer from LaoDa581 on Stack Overflow
🌐
GitHub
github.com › TommyR22 › Android-getcurrentlocation
GitHub - TommyR22/Android-getcurrentlocation: Example to get current location in Android with google maps api v2 and GPS
MainActivity show the map,draw ic_launcher icon in current location and show a toast with coordinate. http://developer.android.com/reference/com/google/android/gms/maps/package-summary.html there are reference for Google Maps to customize your map.
Starred by 3 users
Forked by 5 users
Languages   Java 100.0% | Java 100.0%
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-get-current-location-in-android
How to Get Current Location in Android? - GeeksforGeeks
package org.geeksforgeeks.demo; import android.Manifest; import android.content.pm.PackageManager; import android.location.Location; import android.os.Bundle; import android.widget.Button; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import com.google.android.gms.location.FusedLocationProviderClient; import com.google.android.gms.location.LocationServices; import com.google.android.gms.tasks.OnSuccessListener; public class MainActivity extends AppCompatActivity { // Declare variable
Published   April 7, 2025
🌐
Javapapers
javapapers.com › android › get-current-location-in-android
Get Current Location in Android - Javapapers
This example provides current location update using GPS provider. Entire Android app code is as follows,
🌐
Android Developers
developer.android.com › core areas › sensors and location › get the last known location
Get the last known location | Sensors and location | Android Developers
Learn how to use Google Play services location APIs, specifically the Fused Location Provider, to request the last known location of an Android device, which often represents the user's current location.
🌐
TutorialsPoint
tutorialspoint.com › how-to-get-current-location-latitude-and-longitude-in-android
How to get current location latitude and longitude in Android?
August 30, 2019 - <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" tools:context=".MainActivity"> <TextView android:id="@+id/showLocation" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="Location" android:textSize="24sp" /> <Button android:id="@+id/btnGetLocation" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Get Location" /> </LinearLayout>
🌐
Google
developers.google.com › google play services › fusedlocationproviderclient
FusedLocationProviderClient | Google Play services | Google for Developers
October 31, 2024 - There are several types of use cases for location. One of the most common is simply obtaining a single location in order to determine where the device is now, and continue from there. The getCurrentLocation(CurrentLocationRequest, CancellationToken) API is designed with exactly this use case ...
Find elsewhere
🌐
Narkive
android-beginners.narkive.com › EcLapsW3 › locationmanager-getcurrentlocation
LocationManager.getCurrentLocation
Cheers, Friso this.myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); LocationProvider provider = this.myLocationManager.getProvider("gps"); this.myLocation = myLocationManager.getCurrentLocation(provider.getName()); // Create a new textview to display longitude ...
🌐
DigitalOcean
digitalocean.com › community › tutorials › android-location-api-tracking-gps
Android Location API to track your current location | DigitalOcean
August 3, 2022 - Android Location API can be used to track your mobile current location and show in the app.
🌐
The Crazy Programmer
thecrazyprogrammer.com › home › how to get current location in android using location manager
How to Get Current Location in Android Using Location Manager
January 11, 2017 - <?xml version="1.0" encoding="utf-8"?> ...ion.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Get Current Location" android:id="@+id/getLocationBtn"/> <TextView ...
🌐
W3Docs
w3docs.com › java
How to get current location in Android
import android.location.Location; import android.location.LocationManager; public class MainActivity extends AppCompatActivity { private LocationManager locationManager; private Location currentLocation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); } @Override protected void onResume() { super.onResume(); requestLocationUpdates(); } @Override protected void onPause() { super.onPause(); removeLocationUpdates(); } private void reque
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › android.locations.locationmanager.getcurrentlocation
LocationManager.GetCurrentLocation Method (Android.Locations) | Microsoft Learn
[<Android.Runtime.Register("getCurrentLocation", "(Ljava/lang/String;Landroid/location/LocationRequest;Landroid/os/CancellationSignal;Ljava/util/concurrent/Executor;Ljava/util/function/Consumer;)V", "GetGetCurrentLocation_Ljava_lang_String_Landroid_location_LocationRequest_Landroid_os_CancellationSignal_Ljava_util_concurrent_Executor_Ljava_util_function_Consumer_Handler", ApiSince=31)>] abstract member GetCurrentLocation : string * Android.Locations.LocationRequest * Android.OS.CancellationSignal * Java.Util.Concurrent.IExecutor * Java.Util.Functions.IConsumer -> unit override this.GetCurrentLocation : string * Android.Locations.LocationRequest * Android.OS.CancellationSignal * Java.Util.Concurrent.IExecutor * Java.Util.Functions.IConsumer -> unit
🌐
YouTube
youtube.com › watch
How to Get Current Location on Google Map in Android Studio | Step by Step Google Maps Tutorial - YouTube
In this tutorial video, you will learn how to get your current location on Google Maps in Android Studio. The video provides a step-by-step guide on how to i...
Published   March 23, 2023
🌐
Android Clarified
androidclarified.wordpress.com › 2018 › 08 › 13 › fusedlocationproviderclient-current-location-example
FusedLocationProviderClient | Get Current Location Android Example – Android Clarified
August 12, 2018 - Learn how to fetch current Location with FusedLocationProviderClient. Complete example describing step by step procedure to fetch location using Google APIs
Top answer
1 of 2
6

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
            }

        }
2 of 2
1
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
        }

    }
🌐
GitHub
github.com › TommyR22 › Android-getcurrentlocation › blob › master › README.md
Android-getcurrentlocation/README.md at master · TommyR22/Android-getcurrentlocation
MainActivity show the map,draw ic_launcher icon in current location and show a toast with coordinate. http://developer.android.com/reference/com/google/android/gms/maps/package-summary.html there are reference for Google Maps to customize your map.
Author   TommyR22
🌐
Android Developers
developer.android.com › api reference › locationmanager
LocationManager | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
Medium
sachankapil.medium.com › latest-method-how-to-get-current-location-latitude-and-longitude-in-android-give-support-for-c5132474c864
Latest Method: How to get Current Location (Latitude and Longitude) in Android & Give support for…
January 27, 2023 - How to get the Current Location (Latitude and Longitude) in Android This tutorial helps you to learn How to get the current latitude and longitude in the Android. As a developer when you work on …
🌐
GitHub
github.com › TommyR22 › Android-getcurrentlocation › blob › master › src › com › example › projectname › MainActivity.java
Android-getcurrentlocation/MainActivity.java at master · TommyR22/Android-getcurrentlocation
Example to get current location in Android with google maps api v2 and GPS - Android-getcurrentlocation/MainActivity.java at master · TommyR22/Android-getcurrentlocation
Author   TommyR22