2011-10-06 84 views
2

对于我的应用程序,我必须在Google地图上找到一个点的位置,只知道它位于2个其他点和坐标被捕获时的时间(以毫秒为单位) 。如何找到两个其他点之间的地理点

在我的代码,假设A和B点给出X为找点,我:A和B之间

  1. 计算距离

  2. 筑底时间我发现了的速度(以微度/ MS)从A行进到B

  3. 我发现从点A和点X的距离(使用时间和速度)

  4. 使用类似三角形的规则,我计算纬度和X点的经度从A点

此工作流程带来了错误的地图上,所以,往往在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 

回答

13

你的问题可以通过采取下列步骤来解决。

  • 计算从点A和B(使用Haversine formula,这是足够好的这里,或更复杂的Vincenty formula)的距离。正确使用 式,Android的微度,这是getLatitude 和getLongitude返回,必须使用这样的公式转换为弧度, :从点A

    double radians = Math.toRadians((double)microdegrees/1000000); 
    
  • 计算轴承(方向)和B(在同一页上使用公式 )。这将与毕达哥拉斯公式不同,因为地球是圆形的,而不是平坦的。

  • 然后你可以选择一个新的距离,并计算点X给出 点A和在上一步骤(见或者“目标点一定距离,并从起始点轴承”在同一页上,或Vincenty's direct formula)中发现的轴承。
  • 转换从产生点的弧度为微度使用这个公式:

    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 
+0

你真棒!真的非常感谢你! –

+0

感谢您发布此算法。我需要为我的项目做类似的事情,我相信这样做会的!你介意我把它包装起来并将它作为npm模块发布吗? –

+0

即使超过三年:起首! – 2014-12-11 20:53:18

相关问题