I'm trying to draw a route onto my MapView. I've extended Overlay and implemented the draw() method. The route is displayed properly, although while debugging, I added a breakpoint on draw(), and noticed it is being called constantly.
I only want it to be re-drawn if someone moves the map or zooms (the draw take into account these changes when drawing the route) What can cause this ?
There are two draw methods that can be overridden.
Desc: Draw the overlay over the map.
boolean draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow, long when)
Desc: Draw call for animated overlays.
I originally overrided the second one.
After changing to the first method, it worked out.
In my app, I have an indefinite progress bar (e.g. spinning circle) show when I'm loading network data for the map. When the network data is finished loading, I was setting the progress bar's visibility to invisible. However, this caused the map to continuously redraw as it seems that any animation (I'm guessing) which takes place over the map will cause the map itself to redraw. Simple solution to this is to set the visibility to gone instead. E.g. change this:
to this:
which removes it entirely from the map, no longer animates over it and therefore does not cause draw() to be called a indefinitely.
After further review, the Overlay draw() does actually work as described in the documentation. One draw for shadow = true, and one for shadow = false.
The interesting thing is that the specific overlay draw() method responds as advertized for each element drawn in the MapView. For example, in my case, it seems to respond for each the google map, the Google logo drawn on the map, and then the particular overlay I have drawn myself. Obviously twice for each (shadow true|false).
This is probably the intended way to render the maps. I haven't found or spent enough time researching for this information.
Also, in my own case, I have a transparent panel rendered over my map with CheckBox(s) and TextView widgets. The TextView forces the draw() method to run non-stop since the textview is always listening for input and hence triggering the redrawn of the overlay.
i think the draw method is called repeatedly because the of the background being redrawn constantly. try setting the "MapView.ReticleDrawMode" to DRAW_RETICLE_UNDER. This basically tells the mapView to draw the reticle under the overlays. So overlay's draw method will not be called when the background is recalled. this fixed the issue for me.
for more info, look here: MapView Api
You can simply add this to your ItemizedOverlay class:
This will remove the shadow from your mapview overlay.
For me it looks like a strange bug - sometimes the draw method is called continously, sometimes not.
see here.