Improving Google Maps Performance - android

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

Related

Optimal way to draw lines and polygons dynamically on Nutiteq map?

I have been working on nutiteq maps, and I need to draw a trace over map, as my location changes, but it should be with different width(5m, 10m) and need to calculate where its overlapping itself. I draw lines dinamicly with line.setVertexList(arr_lat_long), but I think that this isn't an good approach to do this job. Think to draw them like a polygons, because then will calculate overlapping with JTS. But it will be very slow operation :/. Is there an another way to draw over the map, some other paint tool or lib to help me. The purpose is to fill some area with these traces..
Thanks

osmdroid trying to display many polygons overlay

i need to overlay many (clickable) polygons on the map (over 5000), so the normal Polygon overlay is slow in performance.
My idea is to create a custom bitmap overlay, drawing on a bitmap canvas only polygons that are inside current bounding coordinates, ignoring all drawing for polygons outside.
Moreover i would to redraw the overlay only when the user has finished to zoom or panning, not during this operation, for performance reasons.
Can someone please point some help to me?
Thanks.
In your case, Bounding box test is a good solution when a lot of polygons are completely outside the viewbox.
You can subclass the Polygon, add a bounding box attribute, and override the draw method to first test if this bounding box is completely outside the viewbox.
No need to draw on a bitmap canvas.
Now, if the user zoom-out, he will have all polygons inside the viewbox: back to the initial issue... A solution could be to handle levels of details. Maybe using the DouglasPeucker reducer (available in OSMBonusPack utils).

Overrinding draw() in customized MapView in Google Maps Android API v2

Very simple question : since MapView inherits from View, is it possible to override draw() to draw whatever you want on the map ?
No, but you can use polylines, polygons, circles. If that's not enough you can put any bitmap (even one you draw to using Canvas) combined with ground overlay. If that's not enough, you can also use tile overlay to draw on entire map efficiently.
It's all there in the documentation, I encourage you to study.
Drawing directly can be always done on a view that overlaps with map, but it won't scroll with map nicely, so better to use technics above.
Drawing directly on the map is impossible, because it doesn't happen in your process afaik.

Overlay.draw() calls many times

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

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

Categories

Resources