Christopher Johnson
Christopher Johnson

Reputation: 2629

Android - Plotting a LOT of lines on google maps - Getting application not responding

I'm writing an app to track movements on google maps v2. For each position changed, I'm adding a new map point to my array, then adding a polyline to the map. I'm also saving the location in a sqlite database. Here is the relevant code:

LatLng mapPoint = new LatLng(location.getLatitude(), location.getLongitude());
            routePoints.add(mapPoint);

            Polyline route = map.addPolyline(new PolylineOptions().color(Color.BLUE).width(2));
            route.setPoints(routePoints);

After about 2000 points, the app becomes unresponsive on my phone. I don't think it's because the array is getting too big because when I pull all of the data from the database (sometimes over 6000 rows), it follows the same logic and paints the map just fine (using the array). I'm wondering if it's because I have everything running on the main thread (music playing, google map, location services, database inserts, textview changes etc). If that's the culprit, how should I change this up to put things in a different thread? What should go in the different thread? And lastly, how would I write up moving any of these things to a different thread (code samples or point me to a resource).

TIA

Upvotes: 0

Views: 652

Answers (1)

Kai
Kai

Reputation: 15476

Ideally you'll want to move everything that doesn't involve the UI into other threads, especially networking and file-accessing (e.g. database) codes. It's probably a death by a thousand cuts situation here. A few suggestions:

  1. Definitely move database/any file access to another thread using AsyncTask and notify your UI to refresh when data have been loaded (confirm you've done this by enabling StrictMode
  2. Minimize database access, since it's backed by flash storage, the same operation can sometimes take 1ms and other times 50ms due to other apps/Android OS itself accessing storage. Once you reads a row, caches it.
  3. If you are using MediaPlayer, either move it to an AsyncTask or call its asynchronous methods (e.g. prepareAsync())
  4. It's also possible that you are issuing too many drawing commands, do some test to see if this is the case. If it is, then only issue draw commands when you know it'll be displayed on screen.

You'll probably want to make your changes in the order presented above.

I don't really know what you are trying to accomplish, but here's a rough outline of how to use AsyncTask:

 private class LocationTask extends AsyncTask<Source, Integer, List<PolylineOptions>> {
     protected Long doInBackground(Source... sources) {
         List<PolylineOptions> list=new ArrayList<PolylineOptions>();

         //create or retrieve Polyline objects here

         return list;
     }

     protected void onProgressUpdate(Integer... progress) {
         //don't need this if it's reasonably fast
     }

     protected void onPostExecute(List<PolylineOptions> result) {
         for(PolylineOptions poly:result) {
             map.addPolyline(poly);
         }
     }
 }

To run: new LocationTask().execute(source1, source2, source3); Source is whatever data structure you use to give LocationTask the ability to perform its function

Upvotes: 1

Related Questions