-1

Ive been trying to get the location of the user for about a week using the fused location API exactly as described over here https://developer.android.com/training/location/retrieve-current.html but im not getting any results.Sometimes it works but mostly it doesnt and this is only on a virtual device, on the real device nothin happens. Here's the my MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                10);
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                10);
        // 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;
    }
    Log.v("","the beg");
    mFusedLocationClient.getLastLocation()
            .addOnSuccessListener(this, new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {
                    // Got last known location. In some rare situations this can be null.
                    if (location != null) {
                        // ...
                        Log.v("", String.valueOf(location.getLatitude()));
                        Log.v("", String.valueOf(location.getLongitude()));
                    }
                }
            });
    Log.v("","the end");
}

} And this my Android.m

hassanito hajj
  • 441
  • 1
  • 4
  • 6
  • @hassanitohajj in the `onSucces` method Do you have this ? `if (location != null) { Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());` .You have some `...` therefore I am just asking. – Catalin Ghita Aug 27 '17 at 23:00

1 Answers1

0

First, define a LocationListener to handle location changes.

private final LocationListener mLocationListener = new LocationListener() {
    @Override
    public void onLocationChanged(final Location location) {
        //your code here
    }
};

Then get the LocationManager and ask for location updates

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
LOCATION_REFRESH_TIME,
            LOCATION_REFRESH_DISTANCE, mLocationListener);
}

And finally make sure that you have added the permission on the Manifest,

For using only network based location use this one

For GPS based location, this one

Appafly
  • 656
  • 2
  • 6
  • 24