Android 如何计算斜边和方位角
我在这个链接上从@DanS获得了下面的代码 以下是数值:Android 如何计算斜边和方位角,android,geolocation,imagemap,bearing,hypotenuse,Android,Geolocation,Imagemap,Bearing,Hypotenuse,我在这个链接上从@DanS获得了下面的代码 以下是数值: 当前:41.850033,-87.6500522999997 左上角:41.866514127810355,-87.6720142364502 右下角:41.83397145565242,-87.62824058532715 地图宽度:512 x 512像素 以下是位置、斜边(距离)、方位(方位)的在线计算器 我得到的结果是: 斜边=2581 轴承=135.21 currentDistanceX=-2562 currentP
- 当前:41.850033,-87.6500522999997
- 左上角:41.866514127810355,-87.6720142364502
- 右下角:41.83397145565242,-87.62824058532715
- 地图宽度:512 x 512像素
- 斜边=2581
- 轴承=135.21
- currentDistanceX=-2562
- currentPixelX=311.9
如果您希望看到实际和预期输出,并希望轻松理解整个画面。请参阅此链接->这是我正在使用的实际代码,而不是以前发布的伪代码:
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()
您介意帮助我如何显示图像地图并标记给定的当前坐标吗?您是否有示例eclipse项目要共享?
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