Android获取位置之间的距离X和Y.

时间:2013-01-25 16:54:40

标签: android

我知道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);
}

3 个答案:

答案 0 :(得分:2)

你能做的就是创造一个中间点:

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

然后,

double distanceX = location1.distance(interm);
double distanceY = interm.distance(location2);

答案 1 :(得分:1)

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

double distY = distance * sin(angle);
double distX = distance * cos(angle);

答案 2 :(得分:0)

Here是您的解决方案。

我希望它有所帮助。