I have a custom map (canvas) that I am drawing a map pointer on. I am updating it in my onLocationChanged method of my main class, however I am struggling to get the bitmap to rotate. getBearing() doesn't seem to work (at least not for me) and so I am working to find the slope between points on the map. Any help would be greatly appreciated.
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);
Edit:
Using latitude and longitude coordinates to find a bearing is more difficult than simply between two points. This code however (modified from code found here) works well:
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));
}