我覆盖了 MyLocationOverlay 的 drawMyLocation 以显示一个箭头作为标记,而不是普通的蓝点。但是,我还需要根据手机的传感器更改指针指向的方向。我决定在 onSensorChanged 中调用 drawMyLocation。但是调用 drawMyLocation 所需的参数之一是 Canvas。如何访问 MyLocationOverlay 的画布?还是每次我在 onSensorChanged 中调用 drawMyLocation 时都需要创建一个新的 Canvas 并传递新的 Canvas?
下面是我尝试覆盖 draw 方法时的代码。但是,即使我可以看到它一直在执行,旋转的位图也需要一段时间才能显示。
**更新:如果我尝试触摸我的地图,我可以看到我的箭头在触摸地图时旋转。但是,如果我不触摸它,箭头需要一段时间才能更新其方向。
public boolean draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow, long when)
{
Log.v("Message", "Executing draw method...");
boolean result;
result = super.draw(canvas, mapView, shadow, when);
if(geoLastFix != null) //geoLastFix contains the GeoPoint of my Location.
{
Point screenPts = mapView.getProjection().toPixels(geoLastFix, null);
//rotate bitmap
Bitmap arrowBitmap = marker;
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
Bitmap rotatedBmp = Bitmap.createBitmap(
arrowBitmap,
0, 0,
arrowBitmap.getWidth(),
arrowBitmap.getHeight(),
matrix,
true
);
//print bitmap to canvas
canvas.drawBitmap(
rotatedBmp,
screenPts.x - (rotatedBmp.getWidth() / 2),
screenPts.y - (rotatedBmp.getHeight() / 2),
null
);
}
return result;
}