Overlay.draw() calls many times

2019-04-12 10:52发布

问题:

I have a question regarding the draw() method of the Overlay class in Android Maps.

When I move map, method draw() get called a few times (from 4 to 13). It's a problem for me, because this method must repaint my route with 70000+ points, and this is a lot resources.

I can't find description for this problem, but when I use code examples from any sources, I have same problem.

回答1:

This is the normal behaviour. When you move the map, you expect it to move smoothly and to achive that any map movement is slipt in smaller movement steps.

For the sake of consitency, the draw() method on the overlay is called for each of this small steps movement, so you can reposition your overlay items to follow each os this steps.

You can improve it using the following:

Improvement 1

For each of the small steps, onDraw is called twice. One with shadow parameter equal to trueand one equal to false. If you are not drawing shadows you can just ignore one of the calls, and therefore reduce the overhead by 2, using the following as the first line of onDraw():

if(shadow) return;

Improvement 2

Optimize the way you are drwaing the route. If you are using canvas.drawLine() you can definitly improve it by using canvas.drawPath(). You create a path with your route just once (for a specific zoom level) and when the map is moved you just offset the path, without the need to recreat it.

Improvement 3

You can even optimize further the path, only adding points that have a distance from previous pixel bigger a speciffic value (i.e. 2 pixels), reducing the total number of points in the path without any visible loss of quality.

I'm using the approach above with routes of several thousand points (aprox 20.000) and the route move smoothly in a medium range device.

Let me know if you need more details in any of the above points.

good luck.



回答2:

I suspect you use

boolean draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow, long when)

and not

void draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow)

The first one is used to draw animations, so it gets called many times.

Ref: draw is being constantly called in my android map overlay