【问题标题】:Fused Location Api setSmallestDisplacement ignored/not workingFused Location Api setSmallestDisplacement 被忽略/不起作用
【发布时间】:2016-07-09 10:37:05
【问题描述】:

我正在尝试创建一个服务,该服务将每 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);

}

}

【问题讨论】:

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


    【解决方案1】:

    根据这个SO question特别是cgr的回答。

    为 LocationRequest 设置的 Displacement,如果设备仍处于静止状态,则无法获取位置,因为 Displacement 优先于 Intevals(interval 和fastestInterval)。我猜到了——也许你已经将不同的 LocationRequest 对象(没有设置位移)传递给 LocationServices.FusedLocationApi.requestLocationUpdates()。

    LocationRequest setSmallestDisplacement (float 最小位移计)

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

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

    查看此page 了解更多信息。

    【讨论】:

    • Deep 如果我需要同时考虑这两个参数,我该怎么办?例如“每 10 秒或 50 米,以先发生者为准。”
    【解决方案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
    }
    

    【讨论】:

    • 不确定这个答案是如何得到支持的。您并没有解决任何问题,尤其是当我们甚至不知道您在 LocationRequest 上设置的值是什么时。
    猜你喜欢
    • 2017-01-06
    • 2016-04-07
    • 2015-12-02
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 2017-05-04
    相关资源
    最近更新 更多