问题描述
我正在开发一款应用,其中一部分是结合传感器计算新位置。
//Compass
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
SensorManager.getRotationMatrixFromVector(rMat,event.values);
mAzimuth = (int) ((Math.todegrees(SensorManager.getorientation(rMat,orientation)[0]) + 360) % 360);
}
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
System.arraycopy(event.values,mLastAccelerometer,event.values.length);
mLastAccelerometerSet = true;
} else {
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
System.arraycopy(event.values,mLastMagnetometer,event.values.length);
mLastMagnetometerSet = true;
}
}
if (mLastMagnetometerSet && mLastAccelerometerSet) {
SensorManager.getRotationMatrix(rMat,null,mLastMagnetometer);
SensorManager.getorientation(rMat,orientation);
mAzimuth = (int) ((Math.todegrees(SensorManager.getorientation(rMat,orientation)[0]) + 360) % 360);
}
mAzimuth = Math.round(mAzimuth);
img_compass.setRotation(-mAzimuth);
String where = "NW";
if (mAzimuth >= 350 || mAzimuth <= 10)
where = "N";
if (mAzimuth < 350 && mAzimuth > 280)
where = "NW";
if (mAzimuth <= 280 && mAzimuth > 260)
where = "W";
if (mAzimuth <= 260 && mAzimuth > 190)
where = "SW";
if (mAzimuth <= 190 && mAzimuth > 170)
where = "S";
if (mAzimuth <= 170 && mAzimuth > 100)
where = "SE";
if (mAzimuth <= 100 && mAzimuth > 80)
where = "E";
if (mAzimuth <= 80 && mAzimuth > 10)
where = "NE";
compass_text.setText(mAzimuth + "° " + where);
}
@Override
public void onAccuracyChanged(Sensor sensor,int accuracy) {
}
public void start() {
if (mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR) == null) {
if (mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) == null
|| mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) == null) {
noSensorAlert();
} else {
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mMagnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
haveSensor = mSensorManager.registerListener(this,mAccelerometer,SensorManager.SENSOR_DELAY_UI);
haveSensor2 = mSensorManager.registerListener(this,mMagnetometer,SensorManager.SENSOR_DELAY_UI);
}
} else {
mRotationV = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
haveSensor = mSensorManager.registerListener(this,mRotationV,SensorManager.SENSOR_DELAY_UI);
}
}
public void noSensorAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this.getActivity());
alertDialog.setMessage("Your device does not suppoert the compass.")
.setCancelable(false)
.setNegativeButton("close",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
getActivity().finish();
}
});
}
public void stop() {
if (haveSensor && haveSensor2) {
mSensorManager.unregisterListener(this,mAccelerometer);
mSensorManager.unregisterListener(this,mMagnetometer);
} else {
if (haveSensor) {
mSensorManager.unregisterListener(this,mRotationV);
}
}
}
我想要实现的:
- 用户从已知位置开始行走 LatLng(X,Y)
- 例如,用户在 5 米后停止行走,现在有了新的位置 -> LatLng(A,B)
- 基于步行方式和指南针数据,我想在谷歌地图上设置新的 Position LatLng(A,B)
如何使用传感器数据计算步行方式以在地图上设置新位置?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)