0

The below code is an extract from my Android application to get current location I have been following http://blog.teamtreehouse.com/beginners-guide-location-android and http://droidmentor.com/get-the-current-location-in-android/ most of the documentation is outdated even the developer.google and developers.android stuff. I want to know why both functions return null and what can I do about it thank you.

  @Override
public void onConnected(Bundle bundle) {
try
        {
            Log.d(TAG, "connection successful");

            mLastLocation = LocationServices.FusedLocationApi
                    .getLastLocation(mGoogleApiClient);

            if (mLastLocation == null) {
                Log.d(TAG, "get last location = null");

               LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); //then nothing happens

            }
            else {
                Log.d(TAG, "last location successful");

                handleNewLocation(mLastLocation);
            }; }


@Override
public void onLocationChanged(Location location){ 
handleNewLocation(location);
}

private void handleNewLocation(Location location) {
    Log.d(TAG, location.toString());

    double currentLatitude = location.getLatitude();
    double currentLongitude = location.getLongitude();

    LatLng latLng = new LatLng(currentLatitude, currentLongitude);

    MarkerOptions options = new MarkerOptions()
            .position(latLng)
            .title("I am here!");

    mMap.addMarker(options);
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));


    Address address = getAddress(location);

    starttext.setText("Current Location: \n" + address.toString());

}
MNS
  • 45
  • 2
  • 2
  • 8
  • 1
    https://stackoverflow.com/q/1513485/115145 – CommonsWare Jan 02 '18 at 23:41
  • 1
    Also, [this portion of the documentation](https://developer.android.com/guide/topics/location/strategies.html) should still be accurate and gets into the behavior of `getLastLocation()` (the `LocationManager` version, anyway -- the fused location API will have similar issues). With regards to `//then nothing happens`, since we do not have your code for `mLocationRequest`, we cannot comment. See [this sample app](https://github.com/commonsguy/cw-omnibus/tree/v8.9/Location/FusedNew) for obtaining and acting on a location fix. – CommonsWare Jan 02 '18 at 23:47
  • @CommonsWare firstly, thank you for replying. Secondly, I have added the code that should be run. As I understand it, requestLocationUpdates calls upon the onLocationChanged. If you require more snippets to better understand the issue, I will provide them. – MNS Jan 03 '18 at 00:03
  • Regarding your statement `both functions return null`, which are the two functions that are returning null? – Daniel Nugent Jan 03 '18 at 01:13
  • @DanielNugent LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); LocationServices.FusedLocationApi .getLastLocation(mGoogleApiClient); – MNS Jan 03 '18 at 04:25

1 Answers1

0

The FusedLocationProviderApi is depreciated https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderApi So use FusedLocationProviderClient instead. Usage: https://developer.android.com/training/location/retrieve-current.html#play-services

Men_on
  • 1
  • 1