This draws an image centered around the given geographical point in a map. Add as many of this as you want to your map.
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
public class DrawableMapOverlay extends Overlay {
private static final double MAX_TAP_DISTANCE_KM = 3;
// Rough approximation - one degree = 50 nautical miles
private static final double MAX_TAP_DISTANCE_DEGREES = MAX_TAP_DISTANCE_KM * 0.5399568 * 50;
private final GeoPoint geoPoint;
private final Context context;
private final int drawable;
/**
* @param context the context in which to display the overlay
* @param geoPoint the geographical point where the overlay is located
* @param drawable the ID of the desired drawable
*/
public CamerasMapOverlay(Context context, GeoPoint geoPoint, int drawable) {
this.context = context;
this.geoPoint = geoPoint;
this.drawable = drawable;
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
super.draw(canvas, mapView, shadow);
// Convert geo coordinates to screen pixels
Point screenPoint = new Point();
mapView.getProjection().toPixels(geoPoint, screenPoint);
// Read the image
Bitmap markerImage = BitmapFactory.decodeResource(context.getResources(), drawable);
// Draw it, centered around the given coordinates
canvas.drawBitmap(markerImage,
screenPoint.x - markerImage.getWidth() / 2,
screenPoint.y - markerImage.getHeight() / 2, null);
return true;
}
@Override
public boolean onTap(GeoPoint p, MapView mapView) {
// Handle tapping on the overlay here
return true;
}
}
In the activity which has your MapView:
MapView map = (MapView) findViewById(R.id.mapview);
MapController mapController = map.getController();
List<Overlay> overlays = map.getOverlays();
overlays.add(new DrawableMapOverlay(this, point, R.drawable.my_drawable);
map.invalidate();
1 Comment
You are calling the constructor "CamerasMapOverlay" instead of DrawableMapOverlay. Just a quibble, but eclipse don't like it...
Kevin