我正在尝试构建一些位置应用程序,主要问题是准确性数据(不确定 getAccuracy() 是否可靠地测量错误)。我在论坛上阅读了一些帖子,其中精度约为 5-10,但对我来说总是在 30 米左右。
目前,我正在使用 fusedlocationproviderclient,我不需要 API 密钥,对吗?Wifi 搜索已启用,否则会跳到 300~ 另一件事,我来自南美洲,可能只是我所在地区的错误更多。
public class MainActivity extends AppCompatActivity {
private FusedLocationProviderClient cliente;
private LocationRequest locationRequest;
private LocationCallback locationCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
locationRequest = new LocationRequest();
locationRequest.setInterval(5000);
locationRequest.setFastestInterval(3000);
locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
gps_bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (gps_bt.isChecked()) {
sensor.setText("GPS");
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
} else {
sensor.setText("Torre celular y wifi");
locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
}
}
});
upd_bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (upd_bt.isChecked()) {
actualizacion.setText("Encendidas");
actualizar = true;
empezarActualizacion();
} else {
actualizacion.setText("Apagadas");
actualizar = false;
detenerActualizacion();
}
}
});
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
for (Location location : locationResult.getLocations()) {
latitud.setText(String.valueOf(location.getLatitude()));
longitud.setText(String.valueOf(location.getLongitude()));
precision.setText(String.valueOf(location.getAccuracy()));
if (location.hasAltitude()) {
altitud.setText(String.valueOf(location.getAltitude()));
} else {
altitud.setText("No se pudo colectar la altitud");
}
if (location.hasSpeed()) {
velocidad.setText(String.valueOf(location.getSpeed()) + "m/s");
} else {
velocidad.setText("No se puede recuperar la velocidad");
}
}
}
};
cliente = LocationServices.getFusedLocationProviderClient(this);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
cliente.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
latitud.setText(String.valueOf(location.getLatitude()));
longitud.setText(String.valueOf(location.getLongitude()));
precision.setText(String.valueOf(location.getAccuracy()));
if (location.hasAltitude()) {
altitud.setText(String.valueOf(location.getAltitude()));
} else {
altitud.setText("No se pudo colectar la altitud");
}
if (location.hasSpeed()) {
velocidad.setText(String.valueOf(location.getSpeed()) + "m/s");
} else {
velocidad.setText("No se puede recuperar la velocidad");
}
}
}
});
} else {
//pedir permisos
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISO_FINE_LOCATION);
}
}
}
它总是抛出 30 米或更多,正如我之前提到的,如果可能的话,我希望达到 10 米左右。清单文件带有FINE_LOCATION。对于未来的使用,这也适用于电话数据(LTE/4g/3g)我是否也必须授予COARSE_LOCATION权限?它 fusedlocationproviderclient 是为此目的的最佳选择吗?使用卡尔曼可以改善纬度/经度数据吗?我在三星 S8 上进行了测试,但我想从棒棒糖中覆盖。谢谢你们。