其他分享
首页 > 其他分享> > android-在MapView上用圆(半径)绘制叠加层

android-在MapView上用圆(半径)绘制叠加层

作者:互联网

我想在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时,我看到一个圆圈,它是我想要的大小,但是随后它增长到屏幕的大小,然后消失了.我该如何解决?
将不胜感激.

解决方法:

好像您每次绘制半径时都使用meterToEquatorPixels重新计算半径.您应该将该计算结果移到构造函数中,看看是否可以解决您的问题.

编辑:

正如奥尔加斯所指出的,投影可以改变.因此,您想要做的就是按原样存储原始半径值,然后在draw方法中执行以下操作:

    float projectedRadius = projection.metersToEquatorPixels(radius);

并在稍后的绘制中使用此值:

    canvas.drawCircle((float)point.x, (float)point.y, projectedRadius, paint);

标签:android-maps,android
来源: https://codeday.me/bug/20191202/2086387.html