I am trying to use FusedLocationProviderClient
to get the Latitude and Longitude of the current location. My code so far is as follows:
Manifest
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Layout: have a button
build.gradle
implementation 'com.google.android.gms:play-services-location:16.0.0'
Java
ArrayList<String> Coord_Array;
Inside OnCreate method
private FusedLocationProviderClient client;
client = LocationServices.getFusedLocationProviderClient( ShowOutletParams.this );
Coord_Array = new ArrayList<String>();
Outside OnCreate method - I am using getLocation()
inside the button onclicklistener
private String getLocation() {
if (ActivityCompat.checkSelfPermission( ShowOutletParams.this, Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission( ShowOutletParams.this, Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED) {
return;
}
client.getLastLocation().addOnSuccessListener( ShowOutletParams.this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
String locat = location.toString();
String lat = locat.substring(15,24);
String longi = locat.substring(25,34);
Coord_Array.add(lat);
Coord_Array.add(longi);
//return Coord_Array;
}
}
} );
}
When I add Log.i
to log the lat and long values, I could see them in the log, however I want them to be saved as strings, which I want to use later.
I understand void
does not return anything....however the Coord_Array
too doesn't return or print the lat and long.