gpt4 book ai didi

java - 计算两个位置之间的像素距离

转载 作者:太空狗 更新时间:2023-10-29 15:01:38 25 4
gpt4 key购买 nike

我正在尝试创建一种方法来计算网格中的 x 和 y,并最终计算该点与中间点之间的距离。问题是,我只知道几个值。为了更好地解释这个案例,一张图片: enter image description here('(..,..)' 之间的值是纬度/经度组合)。

如您所见,我知道以下值:

start of canvas: xy(0,0)
middle of canvas: xy(540,800) and lat/long(52.3702160, 4.8951680)
max dimension of canvas: x 1080, y 1600
point: xy(?,?) and lat/long(52.4167267, 4.8052174)
point: xy(?,?) and lat/long(52,2422306, 5.1129068)

首先,我需要做一些事情来根据点计算缺失的 x 和 y。我已经尝试过执行以下操作:

        double mapWidth    = screenWidth;
double mapHeight = screenHeight;

// get x value
x = (location.getLongitude()+180)*(mapWidth/360);

// convert from degrees to radians
double latRad = location.getLatitude()*Math.PI/180;

// get y value
double mercN = Math.log(Math.tan((Math.PI/4)+(latRad/2)));
y = (mapHeight/2)-(mapWidth*mercN/(2*Math.PI));

point = new PointF((float)x,(float)y);

这有效,但我得到了错误的 x 和 y 值。

例如,如果我的点纬度/经度更远,则 x 和 y 会变大(更靠近中间)。但他们需要更多地站在一边,因为纬度/经度点更远。

直径 2 公里以内的所有纬度/经度点都需要在我的网格中,例如,如果该点距离中心 0.9 公里,则它需要接近侧面。

之后我需要计算两点之间的距离。我已经使用以下方法获得了该部分:

Math.sqrt((point.x - point2.x) * (point.x - point2.x) + (point.y - point2.y) * (point.y - point2.y));

我的主要问题是根据我的纬度/经度点计算 x 和 y。

如果有人想提供帮助,请提前致谢!

最佳答案

我完全重新考虑了计算 x 和 y 的方法。我通过执行以下操作解决了它:

double distanceMeters = mCurrentLocation.distanceTo(location);
x = ((1000+distanceMeters)*mMiddleCoords.x)/1000; //1000 = radius.
y = ((1000+distanceMeters)*mMiddleCoords.y)/1000;

关于java - 计算两个位置之间的像素距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25974911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com