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:
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 ActionBar
is similar to ProgressDialog
, then yes it has to be run in its own thread to work. Luckily, AsyncTask
makes 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 an AsyncTask
. 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()
, and onPostExecute()
all execute on the UI thread, so it's safe to do UI operations there. Check out this snippet:
private class SetMarkersTask extends AsyncTask<Void, Marker, Void>
{
protected void onPreExecute()
{
// Turn progress spinner on
setProgressBarIndeterminateVisibility(false);
}
protected Void doInBackground(Void... params)
{
// Does NOT run on UI thread
// Long-running operation here; create a marker
publishProgress(myMarker);
}
protected void onProgressUpdate(Marker... params)
{
// Add newly-created marker to map
addMarker(params[0]);
}
protected void onPostExecute(Void result)
{
// Turn off progress spinner
setProgressBarIndeterminateVisibility(false);
}
}
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
.