Marker.java

 1package eu.siacs.conversations.ui.widget;
 2
 3import android.graphics.Bitmap;
 4import android.graphics.Canvas;
 5import android.graphics.Point;
 6
 7import org.osmdroid.util.GeoPoint;
 8import org.osmdroid.views.MapView;
 9import org.osmdroid.views.overlay.mylocation.SimpleLocationOverlay;
10
11/**
12 * An immutable marker overlay.
13 */
14public class Marker extends SimpleLocationOverlay {
15	private final GeoPoint position;
16	private final Bitmap icon;
17	private final Point mapPoint;
18
19	/**
20	 * Create a marker overlay which will be drawn at the current Geographical position.
21	 * @param icon A bitmap icon for the marker
22	 * @param position The geographic position where the marker will be drawn (if it is inside the view)
23	 */
24	public Marker(final Bitmap icon, final GeoPoint position) {
25		super(icon);
26		this.icon = icon;
27		this.position = position;
28		this.mapPoint = new Point();
29	}
30
31	/**
32	 * Create a marker overlay which will be drawn centered in the view.
33	 * @param icon A bitmap icon for the marker
34	 */
35	public Marker(final Bitmap icon) {
36		this(icon, null);
37	}
38
39	@Override
40	public void draw(final Canvas c, final MapView view, final boolean shadow) {
41		super.draw(c, view, shadow);
42
43		// If no position was set for the marker, draw it centered in the view.
44		view.getProjection().toPixels(this.position == null ? view.getMapCenter() : position, mapPoint);
45
46		c.drawBitmap(icon,
47				mapPoint.x - icon.getWidth() / 2,
48				mapPoint.y - icon.getHeight(),
49				null);
50
51	}
52}