【发布时间】:2017-01-11 20:11:57
【问题描述】:
我正在尝试创建一个指向特定坐标的箭头,例如指南针。
我正在使用手机中的Sensor.TYPE_ACCELEROMETER 和Sensor.TYPE_MAGNETIC_FIELD 来计算方位角(我参考了这个问题Using orientation sensor to point towards a specific location):
//calculate RotationMatrix
if (gravity != null && geomag != null) {
boolean success = SensorManager.getRotationMatrix(inR, I,
gravity, geomag);
if (success) {
SensorManager.getOrientation(inR, orientVals);
azimuth = Math.toDegrees(orientVals[0]);
pitch = Math.toDegrees(orientVals[1]);
roll = Math.toDegrees(orientVals[2]);
}
}
azimuth += geomagneticField.getDeclination();
//azimuth = Math.round(sensorEvent.values[0]);
float bearing = lastLocation.bearingTo(targetLocation);
float angle = (float) azimuth + bearing;
然后我使用 RotatateAnimation 来创建箭头本身的旋转:
//the +90 below is because the ImageView arrow is pointing towards left by default
RotateAnimation animation = new RotateAnimation(
-(angle+90),
-(angle+90),
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
但是,我测试了四个不同的 targetLocations,每个 targetLocations 分别在东南西北方向,箭头仅正确指向东西位置。对于南北方向的位置,箭头会旋转 180 度,这意味着它将完全指向相反的方向。如果我在旋转中添加 180 度,箭头将正确指向北方或南方的位置,但东方和西方的位置将是错误的。 这真的很令人沮丧,这对我来说完全没有意义。
如果有人能在这里帮助我,我将非常感激!提前致谢!
【问题讨论】:
标签: android rotation location orientation compass-geolocation