# Draw circles of constant size on screen in Google Maps API v2

You probably don't really care about an exact pixel size, just that it looks the same for all zoom levels and device rotations.

Here is a fairly simple way to do this. Draw (and redraw if the zoom is changed) a circle whose radius is some percentage of the diagonal of the visible screen.

The Google Maps API v2 has a getProjection() function that will return the lat/long coordinates of the 4 corners of the visible screen. Then using the super convenient Location class, you can calculate the distance of the diagonal of what is visible on the screen, and use a percentage of that diagonal as the radius of your circle. Your circle will be the same size no matter what the zoom scale is or which way the device is rotated.

Here is the code in Java:

public Circle drawMapCircle(GoogleMap googleMap,LatLng latLng,Circle currentCircle) {

// get 2 of the visible diagonal corners of the map (could also use farRight and nearLeft)

// use the Location class to calculate the distance between the 2 diagonal map points
float results[] = new float[4]; // probably only need 3
Location.distanceBetween(topLeft.latitude,topLeft.longitude,bottomRight.latitude,bottomRight.longitude,results);
float diagonal = results[0];

// use 5% of the diagonal for the radius (gives a 10% circle diameter)
float radius = diagonal / 20;

Circle circle = null;
if (currentCircle != null) {
// change the radius if the circle already exists (result of a zoom change)
circle = currentCircle;

} else {
// draw a new circle
.center(latLng)
.strokeColor(Color.BLACK)
.strokeWidth(2)
.fillColor(Color.LTGRAY));
}

return circle;
}


Use a custom icon for Marker instead. You can create Bitmap and Canvas, draw on the latter and use it as a Marker icon:

new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(bitmap))...


EDIT:

My previous answer is no longer valid. As Jean-Philippe Jodoin brought up, you can simply do that with markers and setting their anchor to 0.5/0.5. It's a way cleaner solution.

Pasting the suggested code snippet here for reference:

marker = mMap.addMarker(new MarkerOptions().position(latlng).anchor(0.5f, 0.5f));


I came accross the same problem and could not find a solution, so I did it myself, I will post in the hope that it is helpful to some other people.

The "marker" approach did not work for me because I wanted circles to be centered on a specific lat/lng, and you cannot do that with a marker: if you set a circle icon for your marker, the circle edge will touch the lat/lng, but the circle will not be centered on the lat/lng.

I created a function to compute what should be the size of the circle in meters given the latitude and the camera zoom level, then added a camera listener on the map to update the size of the circle each time the camera changes zoom level. The result is a circle not changing in size (to the bare eye at least).

Here is my code:

public static double calculateCircleRadiusMeterForMapCircle(final int _targetRadiusDip, final double _circleCenterLatitude,
final float _currentMapZoom) {
//That base value seems to work for computing the meter length of a DIP
final double arbitraryValueForDip = 156000D;
final double oneDipDistance = Math.abs(Math.cos(Math.toRadians(_circleCenterLatitude))) * arbitraryValueForDip / Math.pow(2, _currentMapZoom);
}

//Creating a circle for the example
final CircleOptions co = new CircleOptions();
co.center(new LatLng(0,0));
co.fillColor(Color.BLUE);

//Setting a listener on the map camera to monitor when the camera changes
@Override
public void onCameraMove() {
//Use the function to calculate the radius