在 MapView 上用圆(半径)绘制覆盖图

发布于 2024-12-12 10:10:27 字数 1311 浏览 0 评论 0原文

我想在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 启动时,我看到圆圈,它是我想要的大小,但后来它增长到大小屏幕并消失。我该如何解决这个问题? 将不胜感激任何帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

漫雪独思 2024-12-19 10:10:27

看起来您每次绘制时都使用 metersToEquatorPixels 重新计算半径。您应该将该计算移至构造函数,看看是否可以解决您的问题。

编辑:

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

    float projectedRadius = projection.metersToEquatorPixels(radius);

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

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文