融合的位置Api setSmallestDisplacement被忽略/不起作用

Anninos Kyriakou

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

}

}

自己的

根据这个SO问题,特别是CGR的答案。

如果Device仍处于静止状态,则为LocationRequest设置的Displacement没有机会获取位置,因为Displacement优先于Inteval(interval和fasterInterval)。我猜到了-也许您已将其他LocationRequest对象(未设置位移)传递给LocationServices.FusedLocationApi.requestLocationUpdates()。

LocationRequest setSmallestDisplacement(浮动最小DisplacementMeters)

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

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

检查此页面以获取更多信息。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

融合的位置提供程序setsmallestDisplacement在Android中不起作用

来自分类Dev

融合位置问题 - 地理定位不起作用

来自分类Dev

Android:关闭位置功能后,Google融合位置不起作用

来自分类Dev

Google Maps JS APi v3融合表图层在Chrome中不起作用

来自分类Dev

Android-融合位置API

来自分类Dev

Web API 2 OData = $ format不起作用:始终忽略该请求

来自分类Dev

按钮的位置不起作用

来自分类Dev

背景位置不起作用

来自分类Dev

rails --api不起作用

来自分类Dev

rails --api不起作用

来自分类Dev

REST API 不起作用

来自分类Dev

同步融合Blazor动态查询不起作用

来自分类Dev

位置:粘性在Firefox中不起作用

来自分类Dev

更改UIView的位置不起作用

来自分类Dev

离子变化位置将不起作用

来自分类Dev

位置属性的CSS动画不起作用

来自分类Dev

在后台的位置不起作用

来自分类Dev

JTextArea的位置,setBounds不起作用?

来自分类Dev

JButton设置X位置不起作用

来自分类Dev

SKAudioNode位置音频似乎不起作用

来自分类Dev

更改Bootstrap Datepicker位置不起作用

来自分类Dev

标头位置不起作用

来自分类Dev

phonegap地理位置不起作用

来自分类Dev

CSS Sprite背景位置不起作用

来自分类Dev

位置属性的CSS动画不起作用

来自分类Dev

Nginx拒绝位置不起作用

来自分类Dev

Calabash iOS:设置位置不起作用

来自分类Dev

位置跟踪在GPS中不起作用

来自分类Dev

位置在Firefox中不起作用?