Android Draw Location Accuracy Radius

to get the accuracy in fix meter just use

float accuracy = lm.getLastKnownLocation(best).getAccuracy();

* note: best is an Criteria object

and to draw the Radius, just use this Overlay class, which extend an Overlay Class, and then add it to a MapOverlay

package com.bopbi.component;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Paint.Style;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class SourceOverlay extends Overlay {

	private GeoPoint sourcePoint;
	private float accuracy;

	public SourceOverlay() {
		super();
	}

	public void setSource(GeoPoint geoPoint, float accuracy) {
		sourcePoint = geoPoint;
		this.accuracy = accuracy;
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		super.draw(canvas, mapView, false);
		Projection projection = mapView.getProjection();
		Point center = new Point();

		int radius = (int) (projection.metersToEquatorPixels(accuracy));
		projection.toPixels(sourcePoint, center);

		Paint accuracyPaint = new Paint();
		accuracyPaint.setAntiAlias(true);
		accuracyPaint.setStrokeWidth(2.0f);
		accuracyPaint.setColor(0xff6666ff);
		accuracyPaint.setStyle(Style.STROKE);

		canvas.drawCircle(center.x, center.y, radius, accuracyPaint);

		accuracyPaint.setColor(0x186666ff);
		accuracyPaint.setStyle(Style.FILL);
		canvas.drawCircle(center.x, center.y, radius, accuracyPaint);

	}

}

* note: dont forget to use the setSource() method, before add it to a MapOverlay

About these ads

3 thoughts on “Android Draw Location Accuracy Radius

  1. I have a question about this – doesn’t the default behavior of a GeoPoint already have a accuracy radius circle shown around the point?

    The only issue is that if you use a custom marker, the accuracy circle does not appear. Is your tutorial for this case?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s