我正在开发一个android应用程序,我在Google map中有两个GeoPoints。一个GeoPoint已修复,其他GeoPoint是我当前的位置。 在我目前的位置,我正在修复GeoPoint的方向上放置一个箭头点,因为我的当前位置发生变化,所以箭头将改变方向。它工作正常,但在大多数情况下,它显示错误的方向。Google地图中两个GeoPoints之间的方向android
这是我的代码。
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);
等待您的帮助,并提前致谢。
Altaf
我已经改变了箭头的方向,并使用Math.atan2仍然不能正常工作。准确性有所提高,但仍然在某些情况下,它没有正确显示方向 – Altaf
它有多少'关',以及在什么情况下? – Geobits
轴承怎么样? Location.BearingTo(位置2)?? – MSaudi