位置为null Android

时间:2015-10-19 14:51:25

标签: android geolocation android-location locationlistener

所有我想要获取设备的位置,但它总是返回null。我知道它第一次会返回null但一次又一次返回null。以下是我的代码。请帮我解决这个问题。

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);
        }
  }
}

提前致谢

1 个答案:

答案 0 :(得分:1)

看来,当您LocationClient连接时,您尝试获取最后一个位置。但您需要请求位置更新才能接收当前位置,而不是致电getLastLocation()。您在LocationRequest方法中创建了自己的createLocationRequest(),但您还没有在代码中使用它。

示例代码:

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

<强>更新

当您在yourLocationListener收到新位置时,您需要停止接收位置更新:

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        }
};

您可以找到更多详细信息here

希望它会对你有所帮助。