我知道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);
}
答案 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是您的解决方案。
我希望它有所帮助。