@Override
public void onSensorChanged(SensorEvent event) {
// get the angle around the z-axis rotated
if (event.sensor == mMagneticSensor) {
System.arraycopy(event.values, 0, mLastMagnetometer, 0, event.values.length);
mLastMagnetometerSet = true;
} else if (event.sensor == mAccelerometer) {
System.arraycopy(event.values, 0, mLastAccelerometer, 0, event.values.length);
mLastAccelerometerSet = true;
}
if (mLastAccelerometerSet && mLastMagnetometerSet) {
SensorManager.getRotationMatrix(mR, null, mLastAccelerometer, mLastMagnetometer);
SensorManager.getOrientation(mR, mOrientation);
float azimuthInRadians = mOrientation[0];
float azimuthInDegress = (float) (Math.toDegrees(azimuthInRadians) + 360) % 360;
mTargetDirection = azimuthInDegress;
}
float azimuth = -mTargetDirection;
GeomagneticField geoField = new GeomagneticField( Double
.valueOf( mLastLocation.getLatitude() ).floatValue(), Double
.valueOf( mLastLocation.getLongitude() ).floatValue(),
Double.valueOf( mLastLocation.getAltitude() ).floatValue(),
System.currentTimeMillis() );
if(geoField.getDeclination()<0) {
azimuth += geoField.getDeclination(); // converts magnetic north into true north
}else{
azimuth -= geoField.getDeclination();
}
//Correct the azimuth
azimuth = (azimuth+360) % 360;
//get the bearing
float y = (float)Math.sin(Math.toRadians(target.getLongitude()-mLastLocation.getLongitude())) * (float)Math.cos(Math.toRadians(target.getLatitude()));
float x = (float)Math.cos(Math.toRadians(mLastLocation.getLatitude()))*(float)Math.sin(Math.toRadians(target.getLatitude())) -
(float)Math.sin(Math.toRadians(mLastLocation.getLatitude()))*(float)Math.cos(Math.toRadians(target.getLatitude()))*(float)Math.cos(Math.toRadians(target.getLongitude()-mLastLocation.getLongitude()));
float bearing = (float)Math.toDegrees(Math.atan2(y, x));
image.setRotation(azimuth+bearing);
}
Route.java 文件源码
java
阅读 40
收藏 0
点赞 0
评论 0
项目:GPSTracker
作者:
评论列表
文章目录