我怎么能画一个箭头显示在图形页面行驶方向? [英] How can I draw an Arrow showing the driving direction in MapView?
问题描述
我用谷歌地图组件图形页面
在Android应用程序。我可以使用GPS位置用一个圆点显示我的位置。不过,我想显示一个箭头来代替,这指出了行驶方向(轴承)。我想,我可以使用轴承
价值得到箭头的角度。
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.
我怎么能这样做?
推荐答案
假设你已经得到的位置,然后获得轴承做:
Assuming you've got the Location then obtain the bearing by doing:
float myBearing = location.getBearing();
要实现你将要使用的覆盖<一href="http://$c$c.google.com/android/add-ons/google-apis/reference/com/google/android/maps/ItemizedOverlay.html">ItemizedOverlay和<一href="http://$c$c.google.com/android/add-ons/google-apis/reference/com/google/android/maps/OverlayItem.html">OverlayItem.你需要继承OverlayItem添加到旋转绘制对象的功能。是这样的:
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 void 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);
}
然后,所有剩下来要做的就是将这种新的可绘制应用到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.
这篇关于我怎么能画一个箭头显示在图形页面行驶方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!