融合位置 Api setSmallestDisplacement ignored/not 工作
Fused Location Api setSmallestDisplacement ignored/not working
我正在尝试创建一个服务,它将在每 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);
}
}
据此尤其是cgr的回答
为 LocationRequest 设置的 Displacement,如果设备静止不动则无法获取位置,因为 Displacement 优先于 Intevals(间隔和 fastestInterval)。正如我所猜测的那样 - 也许您已将不同的 LocationRequest 对象(未设置位移)传递给 LocationServices.FusedLocationApi.requestLocationUpdates().
The LocationRequest setSmallestDisplacement (float
smallestDisplacementMeters)
设置位置更新之间的最小位移(以米为单位)。默认情况下,this 的值为 0。它 returns 同一个对象,因此可以链接 setter。
NOTE: Location requests from applications with ACCESS_COARSE_LOCATION
and not ACCESS_FINE_LOCATION will be automatically throttled to a
slower interval, and the location object will be obfuscated to only
show a coarse level of accuracy.
查看此 page 了解更多信息。
尝试这样的事情:
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
}
我正在尝试创建一个服务,它将在每 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);
}
}
据此
为 LocationRequest 设置的 Displacement,如果设备静止不动则无法获取位置,因为 Displacement 优先于 Intevals(间隔和 fastestInterval)。正如我所猜测的那样 - 也许您已将不同的 LocationRequest 对象(未设置位移)传递给 LocationServices.FusedLocationApi.requestLocationUpdates().
The LocationRequest setSmallestDisplacement (float smallestDisplacementMeters)
设置位置更新之间的最小位移(以米为单位)。默认情况下,this 的值为 0。它 returns 同一个对象,因此可以链接 setter。
NOTE: Location requests from applications with ACCESS_COARSE_LOCATION and not ACCESS_FINE_LOCATION will be automatically throttled to a slower interval, and the location object will be obfuscated to only show a coarse level of accuracy.
查看此 page 了解更多信息。
尝试这样的事情:
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
}