在具有数千个地理点的地图中高效绘制路径。
我正在使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
真正让我印象深刻的前两件事是 i
i
i
i
i
i
i
i < mGeoPoints.size();
和Point point = new Point();
您可以通过将数组的长度存储在局部变量中来优化这两者,并且重要的是重用变量点。每个绘制循环创建一个新实例 2000 次会将您的手机锤成小碎片!
还要确保您的 getProjection(...) 方法重用传入的点。只要正确重置点值,您就可以使用如下所示的方法:
The first 2 things that really jump out at me are
i < mGeoPoints.size();
andPoint 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: