public class MyLocationOverlay extends Overlay {
private Canvas canvas;
private final Handler handler = new Handler();
private Runnable runnable;
private GeoPoint geoPoint;
private int xMarker, yMarker;
private int markerNumber = 0;
private Bitmap marker, marker0, marker1, marker2, marker3;
public MyLocationOverlay(Context context) {
Resources r = context.getResources(); = BitmapFactory.decodeResource(r, R.drawable.marker);
marker1 = BitmapFactory.decodeResource(r, R.drawable.marker1);
marker2 = BitmapFactory.decodeResource(r, R.drawable.marker2);
marker3 = BitmapFactory.decodeResource(r, R.drawable.marker3);
marker = marker0;
}
/** 设置 位置*/
public void setLocation(GeoPoint geoPoint) {
this.geoPoint = geoPoint;
}
/** 设置标记是动态还是静态*/
public void animateMarker(boolean on) {
if (on) { // activate animation
if (runnable == null) runnable = new Runnable() {
public void run() {
if (canvas!=null) { switch (markerNumber) {
case 0: marker = marker0; break;
case 1: marker = marker1; break;
case 2: marker = marker2; break;
case 3: marker = marker3; break;
}
markerNumber = ++markerNumber % 4;
canvas.drawBitmap(marker, xMarker, yMarker, null);
mapView.invalidate(xMarker, yMarker,
xMarker+wMarker, yMarker+hMarker);
}
handler.postDelayed(this, 500);
}
};
handler.postDelayed(runnable, 500);
} else { // 静态
marker0
handler.removeCallbacks(runnable);
runnable = null;
marker = marker0;
}
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
if (shadow == false) {
Projection projection = mapView.getProjection();
// 转换坐标到屏幕像素
Point point = projection.toPixels(geoPoint, null);
// 记录canvas 和 标记位置
this.canvas = canvas;
xMarker = point.x - marker.getWidth()/2;
yMarker = point.y - marker.getHeight()/2;
// 绘画标记 在位置的中心
canvas.drawBitmap(marker, xMarker, yMarker, null);
}
super.draw(canvas, mapView, shadow);
}
@Override
public boolean onTap(GeoPoint point, MapView mapView) { return false; }
}
然后在
MapActivity.onResume() 中调用locationOverlay = MyLocationOverlay(this)
GeoPoint point = new GeoPoint(latE6, lonE6);
locationOverlay.setLocation(point);
locationOverlay.animateMarker(true);
mapView.getController().animateTo(point);