Skip to content


Drawing on Google Maps Android API v2

This post is a recipe for drawing various things on the Google MapView on Android. It assumes that you have a MapView (or MapFragment) set up per this post

Code below assumes that mGMap is the google map object, obtained from:

MapFragment mf = (MapFragment) (getFragmentManager().findFragmentById(R.id.displayMap));
mGMap = mf.getMap();

Draw Marker

// draw marker
MarkerOptions mp = new MarkerOptions();
mp.position(pos);
mp.title("Test Marker");
mp.draggable(false);
mGMap.addMarker(mp);

Center Camera on Location (and Animate)

// center camera
CameraPosition cp = new CameraPosition.Builder()
	.target(pos)
	.zoom(15)
	.tilt(45)
	.build();
mGMap.animateCamera(CameraUpdateFactory.newCameraPosition(cp));

Draw Line

// draw line
if (prevPos != null) {
	mGMap.addPolyline(new PolylineOptions()
	.add(prevPos, pos)
	.width(5)
	.color(Color.BLUE)
    );
}

Posted in Android. Tagged with , , , .