我需要计算用户移动的速度。所以我们需要两件事来计算速度,即GPS和加速度计。
但两者都有其局限性。
GPS一直无法使用。虽然我得到了当前的信息 用户的位置总是我从网络提供商那里得到的 不是来自GPS。
加速度计不准确。
我应该采用哪种方法?
答案 0 :(得分:0)
加速度计不可靠,现在您可以尝试测试和调整内容以满足您的要求。
点击此处:
https://github.com/bagilevi/android-pedometer
众多功能之一:
根据用户的步长计算距离和速度
答案 1 :(得分:0)
请尝试以下使用 GPS 获取移动速度的方法
public class MainActivity extends AppCompatActivity {
TextView textView;
private FusedLocationProviderClient mFusedLocationClient;
private double lat;
private double lng;
private static int UPDATE_INTERVAL = 1000;
private static int FATEST_INTERVAL = 1000;
private static int DISPLACEMENT = 0;
private double speed = 0.0;
double currentSpeed,kmphSpeed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textView = findViewById(R.id.textView);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
}
@Override
protected void onResume() {
super.onResume();
if (!runtime_permissions()) {
requestLocations();
}
}
@Override
protected void onPause() {
super.onPause();
//stop location updates when Activity is no longer active
if (mFusedLocationClient != null) {
mFusedLocationClient.removeLocationUpdates(mLocationCallback);
}
}
@SuppressLint("MissingPermission")
private void requestLocations() {
LocationRequest mLocationRequest = new LocationRequest();
// mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FATEST_INTERVAL);
mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
}
LocationCallback mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
List<Location> locationList = locationResult.getLocations();
if (locationList.size() > 0) {
//The last location in the list is the newest
Location location = locationList.get(locationList.size() - 1);
Location location2 = locationResult.getLastLocation();
lat = location.getLatitude();
lng = location.getLongitude();
speed = location.getSpeed();
currentSpeed = round(speed,3,BigDecimal.ROUND_HALF_UP);
kmphSpeed = round((currentSpeed*3.6),3,BigDecimal.ROUND_HALF_UP);
//speed in km/h
// speed = (int) ((location.getSpeed() * 3600) / 1000);
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText("speed=== "+speed+"\ncurrentspeed==="+currentSpeed+"\nkmph speed === "+kmphSpeed);
}
});
// Log.i("SensorTestActivity","SPEEDDDDDspeed=== "+speed+" ");
}
}
};
public static double round(double unrounded, int precision, int roundingMode) {
BigDecimal bd = new BigDecimal(unrounded);
BigDecimal rounded = bd.setScale(precision, roundingMode);
return rounded.doubleValue();
}
private boolean runtime_permissions() {
if (Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 100);
return true;
}
return false;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 100) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
onResume();
} else {
runtime_permissions();
}
}
}
}