融合位置即使在设备开启时也能提供超过 2000 的精度 高精度
Fused location giving accuracy more than 2000 even when device is on High accuracy
我创建了全局 class 来获取我的项目的当前位置。每当用户在 Activity.
上按下按钮时,我都会得到 Location
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);
}
}
我在调用 class 时通过界面获取位置。我必须显示 50 米以下的两个设备的距离,但每当我尝试获取彼此相邻的设备的位置时,两者都会产生超过 2000 的精度。由于这种低精度水平,我无法检测到相邻的设备彼此。
请指导我将此精度级别设置为低于 100,以便我可以检测到 59 米以下的设备。
有时,设备需要一些时间才能找到合适的位置(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.
}
我创建了全局 class 来获取我的项目的当前位置。每当用户在 Activity.
上按下按钮时,我都会得到 Locationpublic 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);
}
}
我在调用 class 时通过界面获取位置。我必须显示 50 米以下的两个设备的距离,但每当我尝试获取彼此相邻的设备的位置时,两者都会产生超过 2000 的精度。由于这种低精度水平,我无法检测到相邻的设备彼此。
请指导我将此精度级别设置为低于 100,以便我可以检测到 59 米以下的设备。
有时,设备需要一些时间才能找到合适的位置(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.
}