【问题标题】:Location is null Android位置为空 Android
【发布时间】:2015-10-19 14:51:25
【问题描述】:

我只是想获取设备的位置,但它总是返回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);
        }
  }
}

提前致谢

【问题讨论】:

    标签: android geolocation android-location locationlistener


    【解决方案1】:

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

    示例代码:

    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

    希望对你有帮助。

    【讨论】:

    • 当我通过这个LocationHelper locationHelper = new LocationHelper(this, this);从另一个班级呼叫这个呼叫时,我正在呼叫createLocationRequest()
    • 但是你在哪里调用 requestLocationUpdates 方法?您需要调用此方法,因为在某些情况下,您的最后一个位置将为空。要确定接收位置更新,您必须调用此方法
    • onConnected 我正在打电话
    • 你必须调用这个方法。因为,在某些情况下,您会收到 null。我更新了我的答案。这应该有效。
    • 您调用 startLocationUpdates,但在此方法中您调用 LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);和您调用 LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient) 的 fetcchLocation() 方法;再次......这还不够
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 1970-01-01
    • 2017-08-19
    • 2021-11-20
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多