Reputation:
I want to draw a circle on MapView, i.e. radius. I do it like this:
Overlay to draw circle:
public class RadiusOverlay extends Overlay {
private Context context;
private double latitude;
private double longitude;
private float radius;
public RadiusOverlay(Context context, double latitude, double longitude, float radius){
/*.....................*/
}
public void draw(Canvas canvas, MapView mapView, boolean shadow){
super.draw(canvas, mapView, shadow);
Point point = new Point();
GeoPoint geoPoint = new GeoPoint((int) (latitude *1e6), (int)(longitude * 1e6));
Projection projection = mapView.getProjection();
projection.toPixels(geoPoint, point);
radius = projection.metersToEquatorPixels(radius);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setARGB(100, 100, 100, 100);
canvas.drawCircle((float)point.x, (float)point.y, radius, paint);
}
}
In MapActivity:
List<Overlay> mapOverlays = mapView.getOverlays();
RadiusOverlay radiusOverlay = new RadiusOverlay(this, latitude, longitude, radius);
mapOverlays.add(radiusOverlay);
I see circle when MapActivity starts, and it's the size I intended, but then it grows to the size of the screen and disappears. How do I fix this? Will appreciate any help.
Upvotes: 3
Views: 3012
Reputation: 9908
It looks like you are re-calculating the radius using metersToEquatorPixels
every time you draw it. You should move that calculation to the constructor and see if that fixes your problem.
EDIT:
As Olgas pointed out, the Projection can change. Therefore what you want to do is store the original `radius value as you are, then in your draw method do:
float projectedRadius = projection.metersToEquatorPixels(radius);
and use this value later in draw as well:
canvas.drawCircle((float)point.x, (float)point.y, projectedRadius, paint);
Upvotes: 3