融合定位API setSmallestDisplacement被忽略/不起作用

10
我正在尝试创建一个服务,以便每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个回答

6
根据这个SO问题,尤其是cgr的答案。
如果设备静止不动,则设置为LocationRequest的Displacement无法获得位置,因为Displacement优先于Intevals(interval和fastestInterval)。 我猜测-您可能已将不同的LocationRequest对象(未设置位移)传递给LocationServices.FusedLocationApi.requestLocationUpdates()。
setSmallestDisplacement(float smallestDisplacementMeters)的LocationRequest设置了位置更新之间的最小位移(以米为单位)。默认情况下,此值为0。它返回相同的对象,以便可以链接设置器。
注意:来自ACCESS_COARSE_LOCATION而不是ACCESS_FINE_LOCATION的应用程序的位置请求将自动限制为较慢的间隔,并且位置对象将被混淆,仅显示粗略的准确度级别。
有关更多信息,请查看此页面

深度学习中,如果我需要同时考虑两个参数,我该怎么办?例如,“每10秒或50米,以先达成为准。” - hushed_voice

0

可以尝试这样做:

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
}

2
不确定这个答案是如何得到赞的。特别是当我们甚至不知道您在LocationRequest上设置了什么值时,您并没有解决任何问题。 - Bamerza

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接