[英]How to find a geographic point between two other points
对于我的应用程序,我必须在谷歌地图上找到一个点的位置,只知道它位于其他 2 个点和坐标被捕获的时间(以毫秒为单位)之间。
在我的代码中,假设 A 和 B 作为给定的点,X 作为要查找的点,我:
计算 A 和 B 之间的距离
根据时间,我发现了从 A 到 B 的速度(以微度/毫秒为单位)
我找到了 A 点和 X 点的距离(使用时间和速度)
使用相似三角形的规则,我从点 A 计算点 X 的纬度和经度
此工作流程会在地图上带来错误,因此 X 标记通常不在 A 和 B 标记之间的线上。
我怎样才能让它更好地工作? 是地球的球形有问题吗?
谢谢你们。
这是代码:
int ax = oldPoint.getLatitude();
int ay = oldPoint.getLongitude();
int bx = currentPoint.getLatitude();
int by = currentPoint.getLongitude();
long at = oldPoint.getDataRilevamento(); //get time first point
long bt = currentPoint.getDataRilevamento(); // get time second point
long xt = x.getDate(); // time of point to find
int c1 = bx-ax;
int c2 = by-ay;
double hyp = Math.sqrt(Math.pow(c1, 2) + Math.pow(c2, 2));
double vel = hyp / (bt-at);
double pos = vel*(xt - at);
int posx = (int)((pos*c1)/hyp);
int posy = (int)((pos*c2)/hyp);
x.setLatitude(ax+posx); //set the latitude of X
x.setLongitude(ay+posy); // set the longitude of X
您的问题可以通过以下步骤解决。
计算点 A 和 B 的距离。这种计算被称为解决“逆测地线问题”,这在 CFF Karney 的文章“测地线算法,2012 年”中进行了讨论。下面的代码使用了Haversine 公式,其精度不如Karney 的文章中介绍的算法。要正确使用该公式,必须使用如下公式将 Android 的微度数(即 getLatitude 和 getLongitude 返回值)转换为弧度:
double radians = Math.toRadians((double)microdegrees/1000000);
从 A 点和 B 点计算方位(方向)(使用同一页上的公式)。 这将与毕达哥拉斯公式不同,因为地球是圆的,而不是平的。
然后你可以选择一个新的距离并计算点 X 给定点 A 和在上一步中找到的方位。 这称为解决“直接测地线问题”。
使用以下公式将生成点的弧度转换为微度:
int microdegrees = (int)(Math.toDegrees(radians)*1000000);
综上所述,我们有以下功能,我将其置于公共领域:
public static int[] getIntermediatePoint(
int startLatMicroDeg,
int startLonMicroDeg,
int endLatMicroDeg,
int endLonMicroDeg,
double t // How much of the distance to use, from 0 through 1
){
// Convert microdegrees to radians
double alatRad=Math.toRadians((double)startLatMicroDeg/1000000);
double alonRad=Math.toRadians((double)startLonMicroDeg/1000000);
double blatRad=Math.toRadians((double)endLatMicroDeg/1000000);
double blonRad=Math.toRadians((double)endLonMicroDeg/1000000);
// Calculate distance in longitude
double dlon=blonRad-alonRad;
// Calculate common variables
double alatRadSin=Math.sin(alatRad);
double blatRadSin=Math.sin(blatRad);
double alatRadCos=Math.cos(alatRad);
double blatRadCos=Math.cos(blatRad);
double dlonCos=Math.cos(dlon);
// Find distance from A to B
double distance=Math.acos(alatRadSin*blatRadSin +
alatRadCos*blatRadCos *
dlonCos);
// Find bearing from A to B
double bearing=Math.atan2(
Math.sin(dlon) * blatRadCos,
alatRadCos*blatRadSin -
alatRadSin*blatRadCos*dlonCos);
// Find new point
double angularDistance=distance*t;
double angDistSin=Math.sin(angularDistance);
double angDistCos=Math.cos(angularDistance);
double xlatRad = Math.asin( alatRadSin*angDistCos +
alatRadCos*angDistSin*Math.cos(bearing) );
double xlonRad = alonRad + Math.atan2(
Math.sin(bearing)*angDistSin*alatRadCos,
angDistCos-alatRadSin*Math.sin(xlatRad));
// Convert radians to microdegrees
int xlat=(int)Math.round(Math.toDegrees(xlatRad)*1000000);
int xlon=(int)Math.round(Math.toDegrees(xlonRad)*1000000);
if(xlat>90000000)xlat=90000000;
if(xlat<-90000000)xlat=-90000000;
while(xlon>180000000)xlon-=360000000;
while(xlon<=-180000000)xlon+=360000000;
return new int[]{xlat,xlon};
}
这是它的使用方法:
int ax = oldPoint.getLatitude();
int ay = oldPoint.getLongitude();
int bx = currentPoint.getLatitude();
int by = currentPoint.getLongitude();
long at = oldPoint.getDataRilevamento(); //get time first point
long bt = currentPoint.getDataRilevamento(); // get time second point
long xt = x.getDate(); // time of point to find
// Find relative time from point A to point B
double t=(bt==at) ? 0 : ((double)(xt-at))/((double)(bt-at));
// Find new point given the start and end points and the relative time
int[] xpos=getIntermediatePoint(ax,ay,bx,by,t);
x.setLatitude(xpos[0]); //set the latitude of X
x.setLongitude(xpos[1]); // set the longitude of X
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.