我有一个用 OpenGL 绘制的 3D 箭头,它指向坐标 (0, 0, 0),我希望它根据我的 GPS 位置和方向指向特定的 GPS 位置。
我尝试计算方位角(使用手机的方向)并将其调整为真正的北方(而不是磁北)。
SensorManager.getOrientation(remappedRotationMatrix, orientation);
// convert radians to degrees
float azimuth = orientation[0];
azimuth = azimuth * 360 / (2 * (float) Math.PI);
GeomagneticField geoField = new GeomagneticField(
Double.valueOf(loc.getLatitude()).floatValue(),
Double.valueOf(loc.getLongitude()).floatValue(),
Double.valueOf(loc.getAltitude()).floatValue(),
System.currentTimeMillis());
// converts magnetic north into true north
azimuth -= geoField.getDeclination();
然后将方位从我的位置获取到我想要指向的位置。
target.setLatitude(42.806484);
target.setLongitude(-1.632482);
float bearing = loc.bearingTo(target); // (it's already in degrees)
if (bearing < 0) {
bearing = bearing + 360;
}
float degrees = bearing - azimuth;
if (degrees < 0) {
degrees = degrees + 360;
}
并计算我必须旋转箭头的度数
gl.glRotatef(degrees, 0.0f, 1.0f, 0.0f);
arrow.draw(gl);
有什么办法吗?另一种可能性是将 GPS 位置转换为 OpenGL 坐标并使用 GLU.gluLookAt 指向它吗?
谢谢。