Adding items to cluster manager & call to cluster() have no effect until AsyncTask finishes

僤鯓⒐⒋嵵緔 提交于 2019-12-13 07:25:27

问题


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:


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.



来源:https://stackoverflow.com/questions/25190152/adding-items-to-cluster-manager-call-to-cluster-have-no-effect-until-asyncta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!