Android callback upon completion of map clustering

I use Google Maps and my clustering utility in my application. Clustering itself works fine, but I have a problem when I try to deal with configuration changes.

This is my case:

  • The user sees a map on the screen, which is fragment .
  • When clustering is complete, markers appear on the map. User can interact with markers .
  • When the user selects a marker , it is highlighted and the BottomSheet expands to display.

If the user rotates the screen (i.e. a configuration change occurs), I save which marker was selected using onSaveInstanceState (in fact, I do not save the marker itself, but only a link to the corresponding List entry, such as ID). Then I want to restore this user selection that was before the configuration change.

Clustering itself is performed as follows:

 clusterManager.clearItems(); clusterManager.addItems(eventManager.getEventList()); clusterManager.cluster(); 

This code is executed when data is received from the server. When clustering is done, all markers are obviously recreated as well. Therefore, in order to highlight the previous user selection (previous marker ), I need to know WHEN the clustering program finishes its work. By then, I'm sure I can safely use features such as:

 clusterManager.getRenderer()).getMarker(<param>) 

and

 clusterManager.getRenderer()).getClusterItem(<param>) 

otherwise, they return null sometimes if the clustering task has not yet completed.

However, I cannot find a reasonable way how to get a response from the clustering utility (i.e. ClusterManager ) when the clustering is complete. I think I need to update this standard clustering code:

  /** * Runs the clustering algorithm in a background thread, then re-paints when results come back. */ private class ClusterTask extends AsyncTask<Float, Void, Set<? extends Cluster<T>>> { @Override protected Set<? extends Cluster<T>> doInBackground(Float... zoom) { mAlgorithmLock.readLock().lock(); try { return mAlgorithm.getClusters(zoom[0]); } finally { mAlgorithmLock.readLock().unlock(); } } @Override protected void onPostExecute(Set<? extends Cluster<T>> clusters) { mRenderer.onClustersChanged(clusters); } } 

I think that onPostExecute should provide a response not only to Renderer , but also to the ClusterManager user (e.g. my fragment ) who is clustering. But I do not want to change the standard code.

Is there a better way to handle this case?

+5
source share
1 answer

You are doing it wrong.

You must implement:

 class YourClusterItemRenderer extends DefaultClusterRenderer<YourClusterItem> 

and install it in ClusterManager as a visualization tool as follows:

 mClusterManager.setRenderer(new YourClusterItemRenderer(...)); 

Override the methods YourClusterItemRenderer onBeforeClusterItemRendered and onBeforeClusterRendered , and you can change how each token looks according to your logic according to the parameters of your server.

So, when ClusterManager finishes rendering, all of your tokens already look as if you want them according to the state of the user, etc.

0
source

Source: https://habr.com/ru/post/1257381/


All Articles