11

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

我怎样才能做到这一点?

4

1 回答 1

19

假设您已经获得了 Location 然后通过执行以下操作获取方位:

float myBearing = location.getBearing();

要实现覆盖,您将使用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); 
}

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

于 2010-12-02T06:23:34.720 回答