繁体   English   中英

Android中Google地图中两个GeoPoints之间的方向

[英]Direction between two GeoPoints in Google map in android

我正在开发一个android应用程序,我在Google地图中有两个GeoPoints。一个GeoPoint是修复的,另一个GeoPoint是我当前的位置。 在我当前的位置上,我将一个箭头指向固定GeoPoint的方向,因为我的当前位置发生变化,因此箭头将更改其方向。它可以正常工作,但在大多数情况下显示的方向是错误的。

这是我的代码。

            Paint paint = new Paint();
        GeoPoint gp1;
        GeoPoint gp2;
        Point point1,point2;
        Projection projection = mapView.getProjection();
        projection.toPixels(gp1, point1);
        projection.toPixels(gp2, point2);
        Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.arrow);
        double dlon = gp2.getLongitudeE6() - gp1.getLongitudeE6();
        double dlat = gp2.getLatitudeE6() - gp1.getLatitudeE6();
        double a = (Math.sin(dlat / 2) * Math.sin(dlat / 2)) + Math.cos(gp1.getLatitudeE6()) * Math.cos(gp2.getLatitudeE6()) * (Math.sin(dlon / 2) * Math.sin(dlon / 2));
        double angle = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        Matrix matrix = new Matrix();
        matrix.postTranslate(-25, -25);
        matrix.postRotate(convertToRadians(angle));
        matrix.postTranslate(point1.x, point1.y);       
        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        canvas.drawBitmap(bmp, matrix, paint);

等待您的帮助并提前致谢。

阿尔塔夫

使用投影点而不是地理位置点进行角度计算,因为您的箭头将显示投影点的方向。

更换

double dlon = gp2.getLongitudeE6() - gp1.getLongitudeE6();
double dlat = gp2.getLatitudeE6() - gp1.getLatitudeE6();

double dlon = point2.x - point1.x;
double dlat = point2.y - point1.y;

对于角度计算,“ Geobits”建议的公式更简单,更好。

double angle = Math.atan2(dlat, dlon);

我彻底测试了此代码,它适用于我的应用程序中的所有情况。

你从哪里得到那个公式的? 对于简单的角度计算,它似乎非常复杂。 通常我只将atan2与y,x一起使用:

double angle = Math.atan2(dlat, dlon); // in radians

假设默认情况下,您的箭头指向水平轴(向东)。 您可能必须通过可绘制箭头所指向的任何方向进行调整。 例如,如果它指向恒定的90度角,只需将其旋转额外的0.5 * PI rad以使其对齐即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM