融合位置Api setSmallestDisplacement忽略/无效

时间:2016-03-22 13:46:16

标签: android google-play-services fusedlocationproviderapi android-fusedlocation

我正在尝试创建一个服务,每x秒和y距离后都会收到位置更新。我在x秒后收到更新,但从未离开y距离。我用不同的值多次测试它,似乎setSmallestDisplacement根本不起作用。有关此事的各种帖子,但没有任何解决方案。如果有人能帮助我,甚至指向不同的方向,我将不胜感激。

我的服务

public class GPS_Service extends Service implements GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener, LocationListener {

private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private int timethresh;
private int distancethresh;
protected Location mCurrentLocation;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    mGoogleApiClient.connect();

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    mLocationRequest = new LocationRequest();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    //Set the desired interval for active location updates, in milliseconds.
    mLocationRequest.setInterval(60* 1000);
    //Explicitly set the fastest interval for location updates, in milliseconds.
    mLocationRequest.setFastestInterval(30* 1000);
    //Set the minimum displacement between location updates in meters
    mLocationRequest.setSmallestDisplacement(1); // float


    return super.onStartCommand(intent, flags, startId);

}

@Override
public void onConnected(Bundle bundle) {
    if (mCurrentLocation == null) {
        mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

    }
     //Requests location updates from the FusedLocationApi.      
    startLocationUpdates();

}

@Override
public void onLocationChanged(Location location) {
    mCurrentLocation = location;
    //Toast.makeText(this, ""+mCurrentLocation.getLatitude()+","+mCurrentLocation.getLongitude(), Toast.LENGTH_SHORT).show();
    System.out.println(""+mCurrentLocation.getLatitude()+","+mCurrentLocation.getLongitude());


}

@Override
public void onConnectionSuspended(int i) {
    // The connection to Google Play services was lost for some reason. We call connect() to
    // attempt to re-establish the connection.
    mGoogleApiClient.connect();
}


@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Toast.makeText(getApplicationContext(),"Location services connection failed with code " + connectionResult.getErrorCode(), Toast.LENGTH_LONG).show();

}

protected void startLocationUpdates() {
    LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, this);

}

}

2 个答案:

答案 0 :(得分:5)

根据这个SO question特别是cgr。

的回答

为LocationRequest设置的Displacement,如果设备仍然是Displacement超过Intevals(interval和fasterInterval),则无法获取位置。我可以猜到 - 也许你已经将不同的LocationRequest对象(没有置换集)传递给LocationServices.FusedLocationApi.requestLocationUpdates()。

  

LocationRequest setSmallestDisplacement(float   smallestDisplacementMeters)

设置位置更新之间的最小位移(米)。默认情况下,this的值为0.它返回相同的对象,以便可以链接setter。

  

注意:来自具有ACCESS_COARSE_LOCATION的应用程序的位置请求   而不是ACCESS_FINE_LOCATION将自动限制为a   较慢的间隔,并且位置对象将仅被混淆   显示出粗略的准确度。

查看此page以获取更多信息。

答案 1 :(得分:2)

尝试这样的事情:

protected void createLocationRequest() {
    mLocationRequest = new LocationRequest();

    mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);

    mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);

    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    mLocationRequest.setSmallestDisplacement(100); //100 meters
}