Android MapView - 箭头覆盖到目标

时间:2012-05-26 06:48:01

标签: android-mapview

我想在指向目标点的源标记处显示一个小箭头作为叠加层。因此,当源位置发生变化时,此箭头应指向所有方向。它就像在指向目的地的源点上显示的指南针。

有什么建议吗?感谢。

1 个答案:

答案 0 :(得分:2)

我发现类似问题的解决方案是使用图像作为代表箭头的标记。然后我计算源和目的地之间的角度,所以我用这个角度旋转标记。 (对不起我的英文)。

protected class yourOverlay extends Overlay{
   public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when){
      super.draw(canvas, mapView, shadow);
      Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.arrow);
      Point sourceCoords = new Point();
      mapView.getProjection().toPixels(sourcePosition, sourceCoords);
      int x = sourceCoords.x - bmp.getWidth()/2;
      int y = sourceCoords.y - bmp.getHeight();
      Point destinationCoords = new Point();
      mapView.getProjection().toPixels(destinationPosition, destinationCoords);
      double angle = Math.toDegrees(Math.atan2(sourceCoords.x - destinationCoords.x, sourceCoords.y - destinationCoords.y));
      Matrix matrix = new Matrix();         
      matrix.postRotate(-(float)angle, bmp.getWidth()/2,bmp.getHeight());
      matrix.postTranslate(x,y);
      canvas.drawBitmap(bmp, matrix, new Paint());
      return true;

    }
}
相关问题