如何在MapView中绘制显示行车方向的箭头?

Jon*_*nas 11 gps android overlay bearing android-mapview

MapView在Android应用程序中使用该Google Maps组件.我可以使用GPS位置用点显示我的位置.但是我想显示一个箭头,指出行驶方向(方位).我认为我可以使用该bearing值来获得箭头的角度.

我怎样才能做到这一点?

phe*_*cks 19

假设您已获得位置,则通过执行以下操作获取方位:

float myBearing = location.getBearing();
Run Code Online (Sandbox Code Playgroud)

要实现叠加层,您将使用ItemizedOverlayOverlayItem.您需要子类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); 
}
Run Code Online (Sandbox Code Playgroud)

然后,剩下要做的就是将这个新的Drawable应用于OverlayItem.这是使用setMarker()方法完成的.

  • 这对我很有用,但是我必须将canvas.getWidth(),canvas.getHeight()更改为canvas.getWidth()/ 2,canvas.getHeight()/ 2以围绕图像的中心旋转(或者它得到旋转不存在) (2认同)