Reputation: 2629
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
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:
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