如何在 MapView 中绘制显示行车方向的箭头? [英] How can I draw an Arrow showing the driving direction in MapView?

查看:31
本文介绍了如何在 MapView 中绘制显示行车方向的箭头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use that Google Maps component MapView in an Android application. I can use the GPS location to show my location with a dot. But I would like to show an arrow instead, that points out the driving direction (bearing). I think that I can use the bearing value to get the angle of the arrow.

我该怎么做?

推荐答案

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

Assuming you've got the Location then obtain the bearing by doing:

float myBearing = location.getBearing();

要实现叠加层,您将使用 ItemizedOverlay叠加项.您需要继承 OverlayItem 以添加旋转 Drawable 的功能.类似的东西:

To implement the overlay you'll be using ItemizedOverlay and OverlayItem. You'll need to subclass OverlayItem to add the functionality to rotate the Drawable. Something like:

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() 方法完成的.

Then all that remains to be done is to apply this new Drawable to the OverlayItem. This is done using the setMarker() method.

这篇关于如何在 MapView 中绘制显示行车方向的箭头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆