How to draw an arrow showing the direction of movement in MapView?

I am using the Google Maps MapView in an Android application. I can use the GPS location to show my location with a point. But instead, I would like to show an arrow indicating the direction of movement (bearing). I think I can use the bearing value to get the angle of the arrow.

How can i do this?

+10
android gps overlay android-mapview bearing
Dec 02 '10 at 4:52
source share
1 answer

Assuming you have a Location, get the bearing by doing:

 float myBearing = location.getBearing(); 

To implement overlay, you will use ItemizedOverlay and OverlayItem . You need to subclass OverlayItem to add functionality to rotate 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); } 

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

+19
Dec 02 '10 at 6:23
source share



All Articles