我在 Android 应用程序中使用 Google map 组件 MapView
。我可以使用 GPS 位置以点显示我的位置。但我想显示一个箭头,指出行驶方向(方位)。我认为我可以使用 bearing
值来获取箭头的角度。
我该怎么做?
最佳答案
假设您已获得位置,然后通过执行以下操作获取方位:
float myBearing = location.getBearing();
要实现覆盖,您将使用 ItemizedOverlay和 OverlayItem .您需要继承 OverlayItem 以添加旋转 Drawable 的功能。像这样的东西:
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);
}
然后剩下要做的就是将这个新的 Drawable 应用到 OverlayItem。这是使用 setMarker() 方法完成的。
关于android - 如何在 MapView 中绘制显示行车方向的箭头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4331794/