Mar*_*ele 2 java maps android latitude-longitude
对于我的应用程序,我必须在谷歌地图上找到一个点的位置,只知道它位于其他2个点和捕获坐标的时间(以毫秒为单位)之间.
在我的代码中,假设A和B为给定点,X为要查找的点,I:
计算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
Run Code Online (Sandbox Code Playgroud)
Pet*_* O. 14
您可以通过以下步骤解决您的问题.
计算从A点和B点开始的距离(使用Haversine公式,这在这里足够好,或者更复杂的Vincenty公式).要正确使用公式,Android的microdegrees(getLatitude和getLongitude返回的值)必须使用如下公式转换为弧度:
double radians = Math.toRadians((double)microdegrees/1000000);
Run Code Online (Sandbox Code Playgroud)从点A和B计算轴承(方向)(使用同一页面上的公式).这与毕达哥拉斯公式不同,因为地球是圆的,而不是平的.
使用以下公式将生成点的弧度转换为微度:
int microdegrees = (int)(Math.toDegrees(radians)*1000000);
Run Code Online (Sandbox Code Playgroud)总而言之,我们有以下功能,我将其置于公共领域:
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};
}
Run Code Online (Sandbox Code Playgroud)
以下是它的使用方法:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3607 次 |
| 最近记录: |