2012-05-26 60 views
0

我想显示一个小箭头作为覆盖从我的来源标记指向目标点。所以这个箭头应该指向所有方向,随着源位置的变化。它将像指向目的地的源点显示的指南针。Android MapView - 箭头覆盖到目的地

有什么建议吗?谢谢。

回答

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; 

    } 
}