【发布时间】:2014-03-19 02:43:49
【问题描述】:
我正在尝试制作一个可以将您指向某个职位的应用。您按下一个按钮,它会存储 gps 坐标,然后计算距离和您需要面对的角度等内容。然后,它会通过使用屏幕上的罗盘图形“指向”它,将您带回到记忆中的位置。
至少,应该如此。在把代码弄乱了几个小时后,我得出的结论是,由于过去几年缺乏三角练习,我只是在某个地方出现了逻辑错误。
指南针和 GPS 位置更新相当频繁。这是我的主要更新调用中的代码,用于旋转指南针并显示距离的用户界面。
public void updateUI(){
double deltaX = targetLongitude - currentLongitude;
double deltaY = targetLatitude - currentLatitude;
double distance = Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2));
double rotation = Math.toDegrees(Math.atan2(deltaX,deltaY));
distanceTextView.setText(Double.toString(distance));
rotateCompass(rotation - degreesClockwiseFromNorth);
}
和rotateCompass的代码:
public void rotateCompass(double degrees){
degrees -= currentRotation; //calculates necessary rotation across updates
currentRotation += degrees;
matrix.postRotate(
(float) degrees,
compass.getDrawable().getBounds().width() / 2,
compass.getDrawable().getBounds().height() / 2);
compass.setImageMatrix(matrix);
}
我几乎可以肯定我的轮换代码有效,因为当我替换时
rotateCompass(rotation - degreesClockwiseFromNorth);
与
rotateCompass(0 - degreesClockwiseFromNorth);
无论我面对的方向如何,它都会在一个真正的指南针旁边指向北方。但是当我使用前者时,它指向一个一致的点,但那个点似乎离目标点很远。 所以我得出的结论是我的错误要么是计算正确的角度,要么是期望 gps 过于精确。我还没有在我的后院测试它的距离,但我认为如果这是一个 gps 精度问题,我会看到我的指南针到处乱跳,而不是顽固地指向错误的方向。
感谢您的阅读,如有任何建议或更正,我们将不胜感激。
【问题讨论】:
-
对这种情况有什么建议吗? stackoverflow.com/questions/68361744/…
标签: android gps geolocation trigonometry