0

我知道location1.distanceTo(location2)

我需要计算

distanceX

distanceY

从两个位置。有没有办法做到这一点?

正确答案基于 njzk2 答案

private Point getDistanceXY(Location location1, Location location2) {

    int angle = (int) location1.bearingTo(location2);

    Location interm  = new Location(location1);
    interm.setLongitude(location2.getLongitude());

    int distanceX = (int) location1.distanceTo(interm);
    int distanceY = (int) location2.distanceTo(interm);

    if (angle<=-90) distanceX=-distanceX;
    else if (angle>-90 && angle<0) {
        distanceX=-distanceX;
        distanceY=-distanceY;
    }

    Log.e("distXY "+name, distanceX+"  "+distanceY+" "+angle);

    return new Point(distanceX, distanceY);
}
4

2 回答 2

2

您可以做的是创建一个中间点:

Location interm = new Location(location1);
iterm.setLongitude(location2.getLongitude());

进而,

double distanceX = location1.distance(interm);
double distanceY = interm.distance(location2);
于 2013-01-25T16:59:46.590 回答
1

您可以结合使用BearingTo()distanceTo()和一点三角函数来计算 X 和 Y 分量。

double distY = distance * sin(angle);
double distX = distance * cos(angle);
于 2013-01-25T16:59:15.030 回答