1

I am using FusedLocationProviderClient to get latitude and longitude on a button click. But when I click the button it shows nothing. Just the app keeps loading for eternity. Can any show me where I am doing wrong in the code?

This is the code:

public class MainActivity extends AppCompatActivity {

    Button showLocation;
    TextView getLat, getLong;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        showLocation = findViewById(R.id.btnShowLocation);
        getLat = findViewById(R.id.getLat);
        getLong = findViewById(R.id.getLong);
    }

    public void getlocation(View v) {
        final ProgressDialog progressDialog=new ProgressDialog(MainActivity.this);
        progressDialog.setMessage("Getting Location");
        progressDialog.setCancelable(false);
        progressDialog.show();
        FusedLocationProviderClient fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(MainActivity.this);
        if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        Task<Location> task = fusedLocationProviderClient.getLastLocation();
        task.addOnSuccessListener(new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                if(location!=null)
                {
                    String lati=String.valueOf(location.getLatitude());
                    getLat.setText(lati);

                    String longi=String.valueOf(location.getLongitude());
                    getLat.setText(longi);

                    Toast.makeText(MainActivity.this,"Location Found !!!",Toast.LENGTH_LONG).show();
                    progressDialog.cancel();
                }
                else
                    Toast.makeText(MainActivity.this,"Please Enable GPS And Internet !!!",Toast.LENGTH_LONG).show();
            }
        });
    }
}
Boken
  • 4,825
  • 10
  • 32
  • 42
Rick
  • 55
  • 1
  • 7
  • your OnSuccessListener is not calling i think . try [this](https://medium.com/@ssaurel/getting-gps-location-on-android-with-fused-location-provider-api-1001eb549089) – Talha Bilal Mar 28 '19 at 11:14
  • Did you check that you didn't get this error `Google Maps Android API: Authorization failure`? – Tamoxin Mar 28 '19 at 13:53
  • No I am not getting any error. When I click the button the progress dialog appears and stays like that forever. – Rick Mar 28 '19 at 14:06

1 Answers1

0

I have used FusedLocation in one of my Kotlin project , i will post the method below.

Make sure that , your location service is enable also the permission for accessing location is already asked from user, else last location cannot be detected.

fun getLocation(): Task<Location>? {
    val mFusedLocationProviderClient: FusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(dashboardActivity)
    if (ActivityCompat.checkSelfPermission(dashboardActivity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(dashboardActivity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    }
    val location = mFusedLocationProviderClient.lastLocation
    location.addOnCompleteListener { task ->
        if (task.isSuccessful) {
            val currentLocation = task.result
            if (currentLocation != null) {
                latitude = currentLocation.latitude
                longitude = currentLocation.longitude
                dashboardActivity.getLatLong(latitude.toString(), longitude.toString()) //this methods calls the collector geo-location service
            } else {

                val builder = AlertDialog.Builder(dashboardActivity)
                builder.setTitle("Location On")

                builder.setPositiveButton("Yes") { dialog, which ->
                    val viewIntent = Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)
                    dashboardActivity.startActivity(viewIntent)
                }
                builder.setNegativeButton("No") { dialog, which ->
                    Toast.makeText(dashboardActivity, "Please enable location service", Toast.LENGTH_SHORT).show()
                }
                val dialog: AlertDialog = builder.create()
                dialog.show()
            }
        }
    }
    return location
}

Instead of using addOnSuccessListener() i have instead used addOnCompleteListener() , i think that might be the issue.

Boken
  • 4,825
  • 10
  • 32
  • 42
iCantC
  • 2,852
  • 1
  • 19
  • 34