How can I draw an Arrow showing the driving direction in MapView?

前端 未结 1 1600
独厮守ぢ
独厮守ぢ 2020-12-05 01:18

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

相关标签:
1条回答
  • 2020-12-05 01:59

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

    float myBearing = location.getBearing();
    

    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); 
    }
    

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

    0 讨论(0)
提交回复
热议问题