Adding items to cluster manager & call to cluster(

2019-08-21 10:52发布

My situation:

I use an AsyncTask to prepare data for a google map. Because this task takes some time, I'd like to display the objects as they are ready (and not all at once when finished). So I use publishProgress&onProgressUpdate to update the map.
I use the Google Maps Android API utility library to add the markers to the map. I call ClusterManager.cluster() after data changes to force reloading of the markers.

The problem:

Nothing happens. Updating a ProgressBar from onProgressUpdate() works flawlessly but displaying the markers on the map does not. I can see that all the methods that are supposed to get called do get called and I've tested that these methods work fine when called not from an AsyncTask. After the AsyncTask finishes, all the markers get magically displayed, even though no further method gets called.
If I use the same method to add markers directly to the map through standard API (i.e. instead of adding them to the cluster manager) the markers appear as they are supposed to.

So the question is - is there any reason for the cluster manager to wait with the re-clustering until the AsyncTask finishes? EDIT: More precisely, not after doInBackground finishes but after onPostExecute finishes.

I'm having trouble understanding and debugging this problem, spent a whole day with it already.
Thanks in advance!

1条回答
做自己的国王
2楼-- · 2019-08-21 11:10

Turns out the problem was in mixing muplitple AsyncTasks - one mine and one in the clustering manager. The fix - not a very nice one - was to make a change in the library (I didn't find a way to nicely extend it because of the private fields) and execute the task clustering task differently in cluster() method:

if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
    mClusterTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
            mMap.getCameraPosition().zoom);
} else {
    mClusterTask.execute(mMap.getCameraPosition().zoom);
}

Found the advice -rather coincitentally- on GitHub.

Hope it might save someone else a headache.

查看更多
登录 后发表回答