在具有数千个地理点的地图中高效绘制路径。

发布于 2024-11-26 09:58:44 字数 1425 浏览 1 评论 0原文

我正在使用MapActivity。我使用 JSON 从网络服务器获取一组 GeoPoints 并将其解析,将折线代码解码为地理点(纬度和经度),然后将其存储在 ArrayList中。

然后将这些点绘制在 MapActivity 上。

我的问题是它运行效率不高,因为我的 ArrayList 大小超过 2000。如何提高效率?

我在绘图时所做的示例代码是

ArrayList'<'Geopoints> mGeoPoints;

mGeoPoints=2000; 的大小

在由 Overlay 扩展的类中 。我正在写的该类的一些方法是。

public boolean draw(Canvas canvas, MapView mapView, boolean shadow,long when) {
       super.draw(canvas, mapView, shadow, when);
        drawPath(mapView, canvas);
        return true;
    }

public void drawPath(MapView mv, Canvas canvas) {
        int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
        Paint paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(3);
                for (int i = 0; i < mGeoPoints.size(); i++) {
            Point point = new Point();
            mv.getProjection().toPixels(mGeoPoints.get(i), point);
            x2 = point.x;
            y2 = point.y;
            if (i > 0) {
                canvas.drawLine(x1, y1, x2, y2, paint);
            }
            x1 = x2;
            y1 = y2;
        }
    }

这段代码运行良好。它在地图上正确绘制。当我缩放地图或移动地图应用程序时挂起。

我已将缩放级别设置为 16 mapController.setZoom(16);;有人可以帮助我吗?即使用户手动缩放它也能快速运行。

I am using a MapActivity. I take a set of GeoPoints from a webserver using JSON and parse it to decoded the Polyline code into geo-points (latitude and longitude) which are then stored in ArrayList<Geopoints>.

These points then get drawn on the MapActivity.

My problem is that it's not running efficiently since my ArrayList<Geopoints> size is more than 2000. How can I increase the efficiency?

Sample code what i ve done while drawing is

ArrayList'<'Geopoints> mGeoPoints;

Say size of mGeoPoints=2000;

In a class extended by Overlay. some methods of that class how i am writing is.

public boolean draw(Canvas canvas, MapView mapView, boolean shadow,long when) {
       super.draw(canvas, mapView, shadow, when);
        drawPath(mapView, canvas);
        return true;
    }

public void drawPath(MapView mv, Canvas canvas) {
        int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
        Paint paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(3);
                for (int i = 0; i < mGeoPoints.size(); i++) {
            Point point = new Point();
            mv.getProjection().toPixels(mGeoPoints.get(i), point);
            x2 = point.x;
            y2 = point.y;
            if (i > 0) {
                canvas.drawLine(x1, y1, x2, y2, paint);
            }
            x1 = x2;
            y1 = y2;
        }
    }

This code is working fine. its drawing correctly on the map. When i zoom the map or move the map application is hanging.

I have set the zoom level to 16 mapController.setZoom(16);; can any one help me? To make it run fast even when user zoom it by hand.

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

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

发布评论

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

评论(1

少女的英雄梦 2024-12-03 09:58:44

真正让我印象深刻的前两件事是 i i i i i i i i < mGeoPoints.size();Point point = new Point();

您可以通过将数组的长度存储在局部变量中来优化这两者,并且重要的是重用变量点。每个绘制循环创建一个新实例 2000 次会将您的手机锤成小碎片!

还要确保您的 getProjection(...) 方法重用传入的点。只要正确重置点值,您就可以使用如下所示的方法:

Point point = new Point();

for (int i = 0; i < mGeoPoints.size(); i++) 
{
    mv.getProjection().toPixels(mGeoPoints.get(i), point);
    x2 = point.x;
    y2 = point.y;
    if (i > 0) 
    {
        canvas.drawLine(x1, y1, x2, y2, paint);
    }
}

The first 2 things that really jump out at me are i < mGeoPoints.size(); and Point point = new Point();

You can optimise both of these right there by storing the length of the array in a local variable and much ore importantly reuse the variable point. Creating a new instance 2000 times per draw loop will hammer your phone into tiny pieces!

Also make sure your getProjection(...) method reuses the point passed in. As long as your point values are reset properly you can use something like this:

Point point = new Point();

for (int i = 0; i < mGeoPoints.size(); i++) 
{
    mv.getProjection().toPixels(mGeoPoints.get(i), point);
    x2 = point.x;
    y2 = point.y;
    if (i > 0) 
    {
        canvas.drawLine(x1, y1, x2, y2, paint);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文