如何在MapView中绘制显示驾驶方向的箭头?
-
30-09-2019 - |
题
我使用Google地图组件 MapView
在Android应用程序中。我可以使用GPS位置显示我的位置。但是我想展示一个箭头,指出了驾驶方向(轴承)。我认为我可以使用 bearing
获得箭头角度的价值。
我怎样才能做到这一点?
解决方案
假设您有位置,然后通过做到轴承:
float myBearing = location.getBearing();
实现您将使用的覆盖层 列出的lay 和 叠加层. 。您需要子类叠加层以添加功能以旋转可绘制的功能。就像是:
public BitmapDrawable rotateDrawable(float angle)
{
Bitmap arrowBitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.map_pin);
// Create blank bitmap of equal size
Bitmap canvasBitmap = arrowBitmap.copy(Bitmap.Config.ARGB_8888, true);
canvasBitmap.eraseColor(0x00000000);
// Create canvas
Canvas canvas = new Canvas(canvasBitmap);
// Create rotation matrix
Matrix rotateMatrix = new Matrix();
rotateMatrix.setRotate(angle, canvas.getWidth()/2, canvas.getHeight()/2);
// Draw bitmap onto canvas using matrix
canvas.drawBitmap(arrowBitmap, rotateMatrix, null);
return new BitmapDrawable(canvasBitmap);
}
然后,剩下的要做的就是将此新绘制应用于叠加层。这是使用setMarker()方法完成的。
不隶属于 StackOverflow