Efficiently drawing path in Maps With More than Thousand GeoPoints.
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.
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);
}
}
精彩评论