I have an Android application that can add 1000's of markers to an Extended GoogleMap using Android Maps Extensions. However i would like to use
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
while the markers are being added to the map which takes 2 - 5 seconds. My problems are:
- I have to add the 1000's markers on the UI thread.
- The indeterminate spinner is also running on the UI thread so it "sticks" while the map markers are being added.
How do i run the indeterminate spinner in its own thread so it isn't affected by the 1000's of markers being added to the Extended map? The extended map settings i am using are these:
final FragmentManager fm = getSupportFragmentManager();
final SupportMapFragment f = (SupportMapFragment) fm.findFragmentById(R.id.map);
mGoogleMap = f.getExtendedMap();
final ClusteringSettings settings = new ClusteringSettings();
settings.iconDataProvider(new TowerIconDataProvider(getResources()));
settings.addMarkersDynamically(true);
mGoogleMap.setClustering(settings);
UPDATE
I discovered the root cause of my addMarker()
performance issue; every timer i added a marker to my GoogleMap i was calling
.icon(BitmapDescriptorFactory.fromResource(R.drawable.my_drawable))
to customise the marker icon
Once I amended my code to perform this outside my addMarker loop, i achieved the desired response time.
Thanks to MaciejGórski for pushing me to really look at my own code.
If the progress spinner in the
ActionBaris similar toProgressDialog, then yes it has to be run in its own thread to work. Luckily,AsyncTaskmakes this really easy. An improvement I would suggest is doing your heavy lifting in a background thread. You could start by doing all of your database lookups and such in anAsyncTask. You can still add the markers on the UI thread, but all of the data crunching will be done on a background thread.onPreExeecute(),onProgressUpdate(), andonPostExecute()all execute on the UI thread, so it's safe to do UI operations there. Check out this snippet:This probably isn't exactly what you need, but it should give you an idea of how to structure your code using the progress spinner inside
ActionBar.