我有一个自定义地图(画布),我正在绘制地图指针。我正在主类的onlocationchanged方法中更新它,但是我正在努力使位图旋转。getBearing()似乎不起作用(至少对我不起作用),所以我正在寻找地图上点之间的坡度。任何帮助都将不胜感激。
public void setBearing(Point prev, Point curr){
float slope = 1;
if (prev.x - curr.x !=0){
slope = (float) ((y1-y2)/(x1-x2));
bearing = (float) Math.atan(slope);
}
}
…
Paint p = new Paint();
Matrix matrix = new Matrix();
matrix.postRotate(bearing, coords.x, coords.y);
Bitmap rotatedImage = Bitmap.createBitmap(image, 0, 0, image.getWidth(),
image.getHeight(), matrix, true);
canvas.drawBitmap(rotatedImage, x-image.getWidth()/2, y-image.getHeight()/2, p);
编辑:
使用经纬度坐标来寻找方位比简单地在两点之间要困难得多。但是,此代码(根据找到的代码进行了修改)工作良好:
public void setBearing(Location one, Location two){
double lat1 = one.getLatitude();
double lon1 = one.getLongitude();
double lat2 = two.getLatitude();
double lon2 = two.getLongitude();
double deltaLon = lon2-lon1;
double y = Math.sin(deltaLon) * Math.cos(lat2);
double x = Math.cos(lat1)*Math.sin(lat2) - Math.sin(lat1)*Math.cos(lat2)*Math.cos(deltaLon);
bearing = (float) Math.toDegrees(Math.atan2(y, x));
}
最佳答案
下面是如何旋转位图:Android: How to rotate a moving animated sprite based on the coordinates of its destination