(我是android的初学者,请对我手下留情)
我正在制作一个应用程序,需要精确定位和计算两个非常接近的位置之间的差异。我非常怀疑GPS能否达到这样的精度,目前我使用的是具有良好加速度计的高端手机。基本上,我如何以极高的精度访问设备的移动量?
发布于 2017-02-20 09:45:44
你可以从这个开始,得到加速度计。我以前没有对这个功能做过任何事情。
public class yourActivity extends Activity implements SensorEventListener{
private SensorManager sensorManager;
double ax,ay,az; // these are the acceleration in x,y and z axis
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
ax=event.values[0];
ay=event.values[1];
az=event.values[2];
}
}
}这里有一个指南的链接,它看起来非常全面,可能会帮助你走上你的道路。Using The Accelerometer
发布于 2017-02-20 10:12:28
看起来不错
private SensorManager sensorMan;
private Sensor accelerometer;
private float[] mGravity;
private float mAccel;
private float mAccelCurrent;
private float mAccelLast;
// In onCreate method
sensorMan = (SensorManager)getSystemService(SENSOR_SERVICE);
accelerometer = sensorMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mAccel = 0.00f;
mAccelCurrent = SensorManager.GRAVITY_EARTH;
mAccelLast = SensorManager.GRAVITY_EARTH;
// And these:
@Override
public void onResume() {
super.onResume();
sensorMan.registerListener(this, accelerometer,
SensorManager.SENSOR_DELAY_UI);
}
@Override
protected void onPause() {
super.onPause();
sensorMan.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
mGravity = event.values.clone();
// Shake detection
float x = mGravity[0];
float y = mGravity[1];
float z = mGravity[2];
mAccelLast = mAccelCurrent;
mAccelCurrent = FloatMath.sqrt(x*x + y*y + z*z);
float delta = mAccelCurrent - mAccelLast;
mAccel = mAccel * 0.9f + delta;
// Make this higher or lower according to how much
// motion you want to detect
if(mAccel > 3){
// do something
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// required method
}https://stackoverflow.com/questions/42335066
复制相似问题