6

Tôi đang cố gắng tạo một dịch vụ sẽ nhận cập nhật vị trí mỗi x giây và sau khoảng cách y. Tôi nhận được cập nhật sau x giây nhưng không bao giờ sau khoảng cách y. Tôi đã thử nghiệm nó nhiều lần với các giá trị khác nhau và có vẻ như setSmallestDisplacement không hoạt động chút nào. Đã có nhiều bài viết về vấn đề đó nhưng không có giải pháp nào. Tôi sẽ đánh giá cao nếu ai đó có thể giúp tôi hoặc thậm chí chỉ cho tôi một hướng khác.Vị trí hợp nhất Api setSmallestDisplacement bị bỏ qua/không hoạt động

Dịch vụ My

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); 

} 

}

Trả lời

4

Theo này SO question đặc biệt là câu trả lời của CGR.

Displacement được đặt cho LocationRequest, không có cơ hội nhận được vị trí nếu thiết bị vẫn là Displacement được ưu tiên hơn Intevals (interval và fastInterval). Như tôi có thể đoán - Có lẽ bạn đã vượt qua đối tượng LocationRequest khác nhau (không có bộ chuyển vị trí) thành LocationServices.FusedLocationApi.requestLocationUpdates().

Các LocationRequest setSmallestDisplacement (float smallestDisplacementMeters)

được thiết lập sự dịch chuyển tối thiểu giữa cập nhật vị trí trong mét. Theo mặc định, giá trị này là 0. Nó trả về cùng một đối tượng, để các bộ lắng có thể bị xâu chuỗi.

GHI CHÚ: yêu cầu Location từ các ứng dụng với ACCESS_COARSE_LOCATION và không ACCESS_FINE_LOCATION sẽ được tự động Throttled để một khoảng chậm hơn, và các đối tượng vị trí sẽ được obfuscated chỉ hiển thị một mức độ thô chính xác.

Kiểm tra điều này page để biết thêm thông tin.

2

Hãy thử một cái gì đó như thế này:

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 
} 
Các vấn đề liên quan