Issue
I have a path() which is created from a json object. I pass the JSON object to my overlay object in my constructor.
Objects that extend Overlay always call draw() any time the map is moved, rendered, touched, anything. Even if your overlay isn't visible or nearby or anything.
I want to generate my path object in a for loop, actually a nested loop as the json object contains nested json arrays. This has a high theoretical computation time, so I don't want to do it in the draw() method. Instead I tried to do the logic in my constructor, to make the path() file, and then call that path file only once in the draw() method where required here canvas.drawPath(path, mPaint);
Unfortunately, when I create my path in the constructor, it does not pan with the map. But when I create it, the exact same code, in the draw() method, it does have the desired functionality: a path that fences a portion of the map.
The problem is that the draw() method will then call my double for loop over and over again, and the performance hit is obvious and debilitating. Even putting the loop in new Thread() within draw() does not help performance. Running it in the constructor would be ideal, but then the path does not pan with the map.
similarly putting a private boolean flip within the draw() method, to make the desired code only run, does not work either. the path will not appear on the map unless it is being constantly redrawn, which is too arduous of a task.
The problem with other answers on this site regarding this issue is that people are making squares, circles and images, which only require one call within draw(), not a loop to generate the path.
suggestions? something about the draw() method helps overlays stick to the map, how can I run my for loop only once
Solution
A few considerations for you:
you should not be overriding the draw() method. The Overlay class already takes care of the whole panning operation for you. Read the docs that you'll find it.
the new API is a few thousand times easier to use. With a simple code such as:
PolylineOptions p = new new PolylineOptions().width(3).color(0xFFEE8888); for( //... first for .... ){ for( // .... how many you'll need to nest... ){ // compute your JSON and get lat/long pair p.add(new LatLng(lat, lon)); } } getMap().addPolyline(p);
and let the map class deal with the panning and zooming.
Answered By - Budius
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.