Overlay.draw() calls many times - android

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.

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.

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

Related

Android: Numerous overlays slowing down map movement

I have numerous Overlays added to a MapView. When I move, zoom in, or zoom out of the MapView the overlays cause a significant lag before the map responds to the action. Is there any way I can minimize this delay?
I saw that someone else suggested combining all the overlays into one large overlay for a similar problem. If something like that would work, how would I combine my array of overlays into a single overlay?
I also saw that it may improve performance to draw all the lines specified in the overlays on a bitmap and then overlay the bitmap instead. If this is the best option, then how would I go about implementing it?
Any help would be greatly appreciated. If you need any more information or code, then please ask!
I have numerous Overlays added to a MapView. When I move, zoom in, or zoom out of the MapView the overlays cause a significant lag before the map responds to the action. Is there any way I can minimize this delay?
Have fewer and less-complicated overlays. Use Traceview to determine exactly where your problem is. If it is that one of your existing draw() methods is slow, you will need to fix that before worrying about combining overlays. If the excess time seems to be in overlay management in MapView, then consolidating your overlays into fewer overlays may have benefit.
how would I combine my array of overlays into a single overlay?
Refactor the code into a single overlay class. Beyond that, we cannot help you, since we do not know much of anything about your overlays, other than that they are "numerous".
If this is the best option, then how would I go about implementing it?
Create a bitmap-backed Canvas, draw your lines on it, then draw the resulting Bitmap in your overlay in draw(), I presume.

Improving Google Maps Performance

I have to draw about 10000 line on Google Maps. So, it is spending too much time in draw() method. Moving on map becomes very laggy. Is there any way to cache drawing or can I draw just the part of map / canvas which is currently seen on screen?
drawing 10000 lines will never get lag free. I'm guessing you connect points.
Here is an implementation of point Clustering in mapView and also renders the visible ones if you want. So you can draw lines to the clustered points.
Now I can draw all 10000 lines without any lag. It is all about designing draw() method carefully. I moved some object creating operations (like Path, Point) out of draw(). I saw that especially projection.toPixels(geoPoint, point); is very expensive operation. Finally I set an alpha constant which holds the pixel value of finger movement. And it only draws when pixelX or pixelY movement is bigger than alpha.
Have a look at this post, it suggests drawing your lines into a shape then drawing that to the mapview.
Here: Cache whats being draw on MapView in Android
Just a suggestion on this one, you may want to try saving the MapView as a bitmap then render that instead (depending on your situation).
Here: Save MapView as a Bitmap

How to Display thousands of OverlayItems in an Android MapView

I have faced some problems with the Android MapView API. I get OverlayItems from a database which I want to display in a MapView. If I'm displaying 100 Icons, I have no issues, but if it gets more - like 500 Items in one City - it first looks really bad, while second it slows down a lot. Unfortunately my goal is to display 10000 of them. I think one solution can be to register a listener to ZoomLevels to make them appear/dissapear, but I couldn't find that functionality. Second, I couldn't find a function to scale my Overlays with the Zoom of the Map.
Any Ideas are very welcome
There is a very strange behavior in ItemizedOverlay draw method. When you say: Draw line from (x,y) to (x1,y1) the draw method is called about 20-30-40 times - i don't know why. It is acceptable when you draw one line, but when you draw a thousands of lines,icons and so on...it is very very bad! To solve this problem you should create a cached overlay. This is overlay that catches the first draw, creates the object and then prevents the future draws that do the same draw.
A cluster is a dozen of icons behind one icon. For example if you have 1000 markers on the map, in a specific minimal zoom level you can not see each marker separately - it becomes a mess of icons and colors and so on. And instead of 100 markers that are very very close one by one you place a cluster marker. And on zoom in remove this cluster and create another clusters...do this until the markers became far enough away and you can seen them divided.
Check this: Cluster markers
Take the following approaches:
Create a cached overlay to prevent multiple drawing of same clusters;
Draw in thread;
Cluster your markers depending on zoom level and marker proximity.
Each time you draw in the overlay, check for sure is the current marker inside of the visible part of the screen. If it is not, do no draw it!
I had a similar problem with the icon size and zoom level in my application. What I ended up doing was having 2 sets of overlays containing the markers, one with a "zoomed in" icon and one with a "zoomed out" icon. Then just changed the overlay at a certain zoom level (using a zoom poller - On zoom event for google maps on android)

Cache whats being draw on MapView in Android

I am developing an app for my Universities campus that displays the campus in a MapView; then using geopoints draws the outlines of the buildings on campus on the mapView using the draw method a class that extents Overlay. There are about 50-60 buildings being drawn, resulting in a very laggy map as the draw method constantly gets drawn over and over.
I have looked into my problem and I have found some people mentioning drawing the buildings on a canvas, but I have found no good examples or info on how to go about implementing this. Can anyone point me in the right direction on how to reduce the map's lag? The map looks very nice but the lag just kills its usefulness.
Thanks!
If you have all the points organized into polygons you can draw polygons on a canvas and then draw it on an Overlay. That i think would be quicker.
Also you can always do some calculation about what part of the building need to be redrawn for the next position and just change that part of the Canvas.
If you moved (X,Y) pixels from an earlier position, shift the existing canvas into the new position and just draw the new things that appear on map.
This is not the optimal solution of course because this kind of caching wouldn't work with the zoom.
Hope it helped somehow!
JQCorreia

More efficient map overlays in Android

In my app I am drawing bus routes on top of a MapView. The routes have anywhere between a dozen and a few hundred GPS coordinates that describe the route that the bus takes.
The problem I'm having is that once I draw out all these lines panning/zooming the MapView is incredibly slow (even clicking the 'Back' button takes a minute to happen).
I'm not sure how relevant it is, but I put in some debug code then checked the logcat output and the MapView is repeatedly calling the draw() method of the Overlay whether anything has changed or not. This is happening several times a second and is causing a massive amount of garbage collection to happen (2-3 MB every second).
Does anyone have any ideas/suggestions for a method to try and speed this up?
I have only used ItemizedOverlay, not Overlay, so these suggestions are pure conjecture. Heck, I haven't even done much with the 2D graphics API.
Obviously, the best answer is to get it to stop calling draw() all the time. Try logging some stack traces and see if you can figure out what is triggering all of the draw() calls. For example, in the Android Google Groups recently, somebody noticed that Chronometer causes widgets in the same UI to be redrawn every second. While I can see you don't have a Chronometer, you might be able to figure out some root cause to the draw() calls that you can correct.
Assuming that does not help, I am guessing that the test for "whether anything has changed or not" is some combination of getLatitudeSpan(), getLongitudeSpan(), getZoomLevel(), and maybe other MapView methods. And, I am assuming that on every draw() you are iterating over your GPS points and drawing the route. If so, you could try:
When you really do draw, draw first to a Canvas backed by a Bitmap, then apply the Bitmap on the Canvas you are handed in draw(), and cache that Bitmap.
Track what combination of values were used in the last draw(), and if the next draw() is the same, just reuse the existing Bitmap. Else, go to step #1, making sure to release the Bitmap (or reuse it, if that's possible).
I am guessing that with graphics acceleration, blasting a Bitmap onto the Canvas is cheaper than iterating over the coordinates and drawing lines. And, by caching the Bitmap, you will save on garbage generation.
Anyway, just a thought.
There are two draw methods in the overlay class. One with 3 arguments and one with 4 arguments. You have to override the draw method with 3 arguments.
Overriding the method with 4 arguments will slow down your application. This is exactly, what happened to me. It seems, where are examples around in the internet with the same error.

Categories

Resources