我想在MapView上绘制一个圆,即半径。我是这样做的:
覆盖以绘制圆圈:
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);
}
}
在MapActivity中:
List<Overlay> mapOverlays = mapView.getOverlays();
RadiusOverlay radiusOverlay = new RadiusOverlay(this, latitude, longitude, radius);
mapOverlays.add(radiusOverlay);
我在MapActivity启动时看到圆圈,它是我想要的尺寸,但随后它会增长到屏幕的大小并消失。我该如何解决? 将不胜感激任何帮助。
答案 0 :(得分:3)
看起来每次绘制时都会使用metersToEquatorPixels
重新计算半径。您应该将该计算移动到构造函数,看看是否能解决您的问题。
编辑:
奥尔加斯指出,投射可以改变。因此,您要做的是按原样存储原始的“半径”值,然后在绘制方法中执行:
float projectedRadius = projection.metersToEquatorPixels(radius);
并在稍后的绘图中使用此值:
canvas.drawCircle((float)point.x, (float)point.y, projectedRadius, paint);