Android MapView上带圆(半径)的图形覆盖
我想在地图视图上画一个圆,即半径。我是这样做的: 覆盖以绘制圆:Android MapView上带圆(半径)的图形覆盖,android,android-maps,Android,Android Maps,我想在地图视图上画一个圆,即半径。我是这样做的: 覆盖以绘制圆: public class RadiusOverlay extends Overlay { private Context context; private double latitude; private double longitude; private float radius; public RadiusOverlay(Context context, double latitud
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);
}
}
List<Overlay> mapOverlays = mapView.getOverlays();
RadiusOverlay radiusOverlay = new RadiusOverlay(this, latitude, longitude, radius);
mapOverlays.add(radiusOverlay);
在地图活动中:
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);
}
}
List<Overlay> mapOverlays = mapView.getOverlays();
RadiusOverlay radiusOverlay = new RadiusOverlay(this, latitude, longitude, radius);
mapOverlays.add(radiusOverlay);
List mapOverlays=mapView.getOverlays();
RadiusOverlay RadiusOverlay=新的RadiusOverlay(此、纬度、经度、半径);
添加(RadiusOverlays);
当MapActivity启动时,我看到了圆圈,它是我想要的大小,但随后它增长到屏幕大小并消失。我该如何解决这个问题?
非常感谢您的帮助。看起来您每次绘制半径时都在使用
米到四像素
重新计算半径。您应该将该计算移到构造函数中,看看这是否解决了您的问题
编辑:
正如奥尔加斯指出的,投影可以改变。因此,您要做的是按原样存储原始`半径值,然后在绘制方法中执行以下操作:
float projectedRadius = projection.metersToEquatorPixels(radius);
并在以后的绘图中使用此值:
canvas.drawCircle((float)point.x, (float)point.y, projectedRadius, paint);
我认为这是错误的。javadoc for getProjection方法说“你不应该在一次以上的绘制中保持这个对象,因为地图的投影可能会改变。”我一直在根据以前的半径值重新计算半径,也就是说,我以像素为单位计算半径(从上一个onDraw()调用),并尝试以像素为单位计算新半径。谢谢你指点我。说得好。我正在编辑我的回复,以反映油气何时开始增长?当您更改缩放级别或可能移动地图时?