Geolocation - Location is null Android

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

all i am trying to get the location of the device but it always returns null. I know for the first time it will return null but again and again it is returning null. Below is my code. Please help me to solve this .

public class LocationHelper implements LocationListener,
     GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {


private static final String TAG = "LocationService";
private static final long INTERVAL = 1000 * 5;
private static final long FASTEST_INTERVAL = 1000 * 3;

Context context;
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
Location mCurrentLocation;
String mLastUpdateTime;
LocationUpdateListener locationUpdateListener;

public LocationHelper(Context context, LocationUpdateListener locationUpdateListener) {
    this.context = context;
    this.locationUpdateListener = locationUpdateListener;

    if (!isGooglePlayServicesAvailable()) {
    } else {
        createLocationRequest();
        mGoogleApiClient = new GoogleApiClient.Builder(context)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
        mGoogleApiClient.connect();
    }
}

/**
 * Create the location request
 */
protected void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(INTERVAL);
    mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
}

private boolean isGooglePlayServicesAvailable() {
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
    if (ConnectionResult.SUCCESS == status) {
        return true;
    }else if(status == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED){
        Log.d(TAG, "please udpate your google play service");
        Log.d(TAG, "please udpate your google play service");
        Log.d(TAG, "please udpate your google play service");
        return true;
    }else {
 //            GooglePlayServicesUtil.getErrorDialog(status, getBaseContext(), 0).show();
        Log.d(TAG, "google play services is not available - ...............:");
        return false;
    }
}

@Override
public void onConnected(Bundle bundle) {
    startLocationUpdates();
}

private void startLocationUpdates() {
    mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
    Log.d(TAG, "Location update started ..............: ");
    fetchLocation();
     Log.d(TAG, "Location update started ..............: ");
      Log.d(TAG, "Location update started ..............: ");
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}

@Override
public void onLocationChanged(Location location) {
    mCurrentLocation = location;
    mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
    fetchLocation();
    Log.v("onLocationChanged","onLocationChanged");
    Log.v("onLocationChanged","onLocationChanged");
    Log.v("onLocationChanged","onLocationChanged");
}

  //Get the location
private void fetchLocation(){
    while (mCurrentLocation == null) {
            try {
                mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
                Log.d(TAG, "Location null ");

                Thread.sleep(2000);
              } catch (Exception ex) {
                    ex.printStackTrace();
            }

         }
        if (mCurrentLocation != null) {
            locationUpdateListener.locationUpdate(mCurrentLocation);
        }
  }
}
  Thanks in advance   

Answers

It seems, that you try to get last location when your LocationClient connected. But you need to request a location update to receive your current location instead of calling getLastLocation(). You created your own LocationRequest in createLocationRequest() method but you haven t ever used it in code.

Example code:

LocationServices.FusedLocationApi.requestLocationUpdates(yourGoogleApiClient, yourLocationRequest, yourLocationListener);
  Update:   

End when you received new location in yourLocationListener you need to you have to stop receiving location updates:

private LocationListener mLocationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
        Log.v("onLocationChanged","onLocationChanged");

        if(mGoogleApiClient.isConnected()) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, mLocationListener);
            mGoogleApiClient.disconnect();
        }

        //your code here        }
};

http://developer.android.com/training/location/receive-location-updates.html http://developer.android.com/training/location/receive-location-updates.html

Hope it will help you.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/33217537/location-is-null-android

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils