Overlaying images onto a MapView and refreshing its position when zoom changes
I have a list of GeoPoints conforming a route. I have created a class to paint a star png image at the start point, and a F1 flag (meta.png) at the end point.
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;
}
}
...
}
At the onCreate method, I proceed like this:
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();
}
...
}
Images appear over the map, but they only appear at the right coordinates when zoom is at its maximum value. When I zoom out, they are imprecise and appear far away from its right place.
How can I make them move to the correct point each time zoom changes? And why do they fit right for the maximum zoom value开发者_如何转开发 if I am starting with value 10?
精彩评论