我有符合路线的GeoPoints列表。我创建了一个类来在起点绘制一个星形png图像,并在结束点绘制一个F1标志(meta.png)。
public class MapTestActivity extends MapActivity {
...
class Marcador extends com.google.android.maps.Overlay {
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(ruta.get(0), screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.start);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
// Otra vez para la meta:
//---translate the GeoPoint to screen pixels---
screenPts = new Point();
mapView.getProjection().toPixels(ruta.get(ruta.size()-1), screenPts);
//---add the marker---
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.meta);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
}
...
}
在onCreate方法中,我这样做:
public class MapTestActivity extends MapActivity {
...
private List<GeoPoint> ruta;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView)findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
MapController mc = mapView.getController();
ruta = new ArrayList<GeoPoint>();
ruta.add(new GeoPoint((int) (43.30782*1E6), (int) (-5.69379*1E6)));
ruta.add(new GeoPoint((int) (43.30785*1E6), (int) (-5.69435*1E6)));
ruta.add(new GeoPoint((int) (43.30832*1E6), (int) (-5.69422*1E6)));
ruta.add(new GeoPoint((int) (43.30894*1E6), (int) (-5.69538*1E6)));
mc.animateTo(ruta.get(0));
mc.setZoom(10);
// PNG images:
Marcador marcadoresSalidaYMeta = new Marcador();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(marcadoresSalidaYMeta);
mapView.invalidate();
}
...
}
图像显示在地图上,但只有当缩放处于最大值时,它们才会显示在右侧坐标处。当我缩小时,它们是不精确的,并且显得远离它的正确位置。
每次缩放变化时如何让它们移动到正确的位置?如果我从值10开始,为什么它们适合最大变焦值?