我创建了用于获取项目当前位置的全局类。每当用户按下Activity上的按钮时,我就会获得位置。
public class Get_Current_Location implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
GoogleApiClient apiClient;
Location CurrentLocation;
LocationRequest locationRequest;
Context context;
Interface_For_Location getLocation;
String TAG = "###Current Location###";
public Get_Current_Location(Context context,Interface_For_Location location) {
this.context = context;
this.getLocation=location;
apiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
Log.d(TAG, "Google API BUILD Successfully " + apiClient);
apiClient.connect();
setLocationRequest();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
Log.d(TAG,"On Connected Calling");
startLocationUpdate();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onLocationChanged(Location location) {
Log.d(TAG,"On Location Changed Calling ");
Log.d(TAG,"Updated Location OnLocationChnaged "+location);
CurrentLocation=location;
getLocation.currentLoc(CurrentLocation);
stopLocationUpdates();
}
public void startLocationUpdate() {
Log.d(TAG,"Start Location Update Calling ");
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(apiClient, locationRequest, this);
}
private void stopLocationUpdates(){
LocationServices.FusedLocationApi.removeLocationUpdates(apiClient,this);
}
private void setLocationRequest(){
Log.d(TAG,"Set Location Update Request Calling");
locationRequest=new LocationRequest();
locationRequest.setInterval(10*60*1000);
locationRequest.setFastestInterval(10*60*1000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setNumUpdates(1);
}
}
我通过调用类的接口获取位置。我必须显示50米以下的两个设备的距离,但每当我试图获得彼此相邻的设备的位置时,两者都产生超过2000的精度。由于这种低精度水平,我无法检测到旁边的设备彼此。
请指导我将此精度水平降至100以下,以便检测59米以下的设备。
答案 0 :(得分:0)
有时,设备需要时间才能获得良好的位置(GPS或网络),因此您可能会在短时间内获得非常不准确的点数。您需要为所需的准确度添加过滤器,并且只有在获得所需的点时才关闭位置更新。以下是如何执行此操作的示例:
@Override
public void onLocationChanged(Location location) {
Log.d(TAG,"On Location Changed Calling ");
if (location.getAccuracy() <= MIN_ACCURACY) {
Log.d(TAG,"Updated Location OnLocationChnaged "+location);
CurrentLocation=location;
getLocation.currentLoc(CurrentLocation);
stopLocationUpdates();
}
//Else don't stop the update until a location with good accuracy is received.
}