Java 计算两个位置之间的距离(以像素为单位)

Java 计算两个位置之间的距离(以像素为单位),java,android,math,location,coordinates,Java,Android,Math,Location,Coordinates,我正在尝试创建一种方法来计算网格中的x和y,并最终计算该点和中间点之间的距离。问题是,我只知道几个值。为了更好地解释这种情况,请使用图像: (在“(..,..”之间的值是横向/纵向组合) 如您所见,我知道以下值: 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(?,

我正在尝试创建一种方法来计算网格中的x和y,并最终计算该点和中间点之间的距离。问题是,我只知道几个值。为了更好地解释这种情况,请使用图像: (在“(..,..”之间的值是横向/纵向组合)

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

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);
Math.sqrt((point.x - point2.x) * (point.x - point2.x) + (point.y - point2.y) * (point.y - point2.y));
double distanceMeters = mCurrentLocation.distanceTo(location);
        x = ((1000+distanceMeters)*mMiddleCoords.x)/1000; //1000 = radius.
        y = ((1000+distanceMeters)*mMiddleCoords.y)/1000;
这是可行的,但是我得到了错误的x和y值

例如,如果我的点lat/long距离更远,则x和y会变得更大(更多地位于中间)。但它们需要更多地位于侧面,因为横向/纵向点距离更远

直径2km内的所有横向/纵向点都需要在我的网格中,例如,如果该点距离中心0.9km,则需要接近侧面

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

        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);
Math.sqrt((point.x - point2.x) * (point.x - point2.x) + (point.y - point2.y) * (point.y - point2.y));
double distanceMeters = mCurrentLocation.distanceTo(location);
        x = ((1000+distanceMeters)*mMiddleCoords.x)/1000; //1000 = radius.
        y = ((1000+distanceMeters)*mMiddleCoords.y)/1000;
我的主要问题是从横向/纵向点计算x和y


如果有人想帮忙,请提前感谢

我完全重新思考了计算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);
Math.sqrt((point.x - point2.x) * (point.x - point2.x) + (point.y - point2.y) * (point.y - point2.y));
double distanceMeters = mCurrentLocation.distanceTo(location);
        x = ((1000+distanceMeters)*mMiddleCoords.x)/1000; //1000 = radius.
        y = ((1000+distanceMeters)*mMiddleCoords.y)/1000;

不能直接使用纬度和经度的距离公式。在计算距离时,必须考虑球体的曲率

球体上两点之间的最小距离(因此地球将其简化为一个完美球体)是通过这些点的大圆上的弦的长度。大圆是一个圆心穿过球体中心的圆)

其中:

X = cos(lat2) * cos(long2) - cos(lat1) * cos(long1)
Y = cos(lat2) * sin(long2) - cos(lat1) * sin(long1)
Z = sin(lat2) - sin(lat1)
距离是
(2*R*arcin(C/2))
其中
R
是地球的半径或
6371 km


另一种选择——如果您知道您将始终拥有Android库的话——是
Location.distanceTo()
方法。

为什么半径是1000?正如我在问题中所说,直径是2km。1km是半径,1000m是以米为单位的半径。你的问题是,你想以像素为单位计算距离,而你需要以公里为单位计算距离。你想计算的是什么?你能澄清一下你的问题吗?X和Y是以像素为单位的。正如我在问题中所说,我已经知道如何计算点之间的距离。在上面的例子中,我计算了第二个点的X和Y,然后计算了中点(我已经计算过)和新点(X,Y)之间的距离。请记住,赤道(纬度0)上经度0和1之间的距离为111km,而北极(纬度90)为零。所以你的公式不正确。