如何计算斜边和方位



我在这个链接上从@DanS那里得到了下面的代码

onCurrentPosition(Location current){
    double hypotenuse = upperLeft.distanceTo(current);
    double bearing = upperLeft.bearingTo(current);
    double currentDistanceX = Math.cos(bearing) * hypotenuse;
    //                     "percentage to mark the position"
    double currentPixelX = (currentDistanceX / upperLeft.distanceTo(lowerRight) * Math.cos(upperLeft.bearingTo(lowerRight))) * mapWidth;
    moveIndicatorX(currentPixelX);
}

值如下:

  • 当前:41.850033、-87.65005229999997
  • upperLeft: 41.866514127810355、-87.6720142364502
  • lowerRight: 41.83397145565242、-87.62824058532715
  • mapWidth: 512 x 512 px

这是在线计算器的位置,斜边(距离),方位(方位角)

  • 将LatLng转换为Location格式(例如:41°51′59.45″n 87°40′19.25″w)
  • 计算距离&;给定位置的方位角

我得到的结果是:

  • 斜边= 2581
  • 轴承= 135.21
  • currentDistanceX = -2562
  • currentPixelX = 311.9

希望你们能:

  1. 确认我的计算结果是否正确。
  2. 如何计算currentPixelY(另一个点)?

顺便说一下,我计划用它来计算给定的现实生活中的LatLng(当前)的位置,与我的静态图像地图相比较,该地图将静态图像的左上角和右下角结合到现实生活中的LatLng中。

如果你想看到实际的&期望的输出和想要轻松理解的全貌。请参考此链接->如何将当前位置标记为静态图像地图

这是我使用的实际代码,而不是以前发布的伪代码:

Location upperLeft = new Location("");
upperLeft.setLatitude(41.866514127810355);
upperLeft.setLongitude(-87.6720142364502);
Location lowerRight = new Location("");
lowerRight.setLatitude(41.83397145565242);
lowerRight.setLongitude(-87.62824058532715);
Location current = new Location("");
current.setLatitude(41.850033);
current.setLongitude(-87.65005229999997);
double hypotenuse = upperLeft.distanceTo(current);
double bearing = upperLeft.bearingTo(current);
double currentDistanceX = Math.cos(bearing * Math.PI / 180.0) * hypotenuse;
//                     "percentage to mark the position"
double totalHypotenuse = upperLeft.distanceTo(lowerRight);
double totalDistanceX = totalHypotenuse * Math.cos(upperLeft.bearingTo(lowerRight) * Math.PI / 180.0);
double currentPixelX = currentDistanceX / totalDistanceX * 512;
System.out.println(currentPixelX); // 259.3345493341548

你计算出来的答案看起来有点不对劲。要计算Y,将所有X标记的计算和变量复制为使用Math.sin()而不是Math.cos()

最新更新