How can I create two listeners? One is used for the clustering and the other will load new cluster items to the map if no cluster items exists on the map.
//needed for clustering
map.setOnCameraChangeListener(mClusterManager);
// need for loading clusteritems
map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition position) {
// ifMarkesNotInsideMapReload...
I think it's not possible but someone maybe solved this...
One idea is to set a starting position and poll for changes manually.. not cool :-)
I'm using googles cluster library: google cluster
Use the Composite Pattern to create a class that both is a CameraChangeListener
and contains CameraChangeListener
s, such that it can be set as the sole listener on the map while delegating to multiple children that do the real work.
In this case, you would create a CompositeCameraChangeListener
class which implements the CameraChangeListener
interface and maintains a list of child listeners it delegates to.
Some pseudocode:
public class CompositeCameraChangeListener implements CameraChangeListener {
private final List<CameraChangeListener> mListeners = new ArrayList<CameraChangeListener>();
@Override
public void onCameraChange(CameraPosition position) {
for (CameraChangeListener listener in mListeners) {
listener.onCameraChange(position);
}
}
// Other methods to add, remove listeners
}
Then in your main class:
CompositeCameraChangeListener composite = new CompositeCameraChangeListener();
composite.add(mClusterManager);
composite.add(new CameraChangeListner() { ... });
map.setOnCameraChangeListener(composite);
Now when the CompositeCameraChangeListener
's method is invoked, it forwards the call to the other listeners and as far as your app is concerned, you now have two (or more) listeners registered on an object that is designed to only accepted one.
I have looked into ClusterManager
implementation of AMUtils library and it looks like it calls onCameraChange
on your implementation of ClusterRenderer
if it also implements OnCameraChangeListener
. Simply make it implement that interface.
Relevant parts of the code:
@Override
public void onCameraChange(CameraPosition cameraPosition) {
if (mRenderer instanceof GoogleMap.OnCameraChangeListener) {
((GoogleMap.OnCameraChangeListener) mRenderer).onCameraChange(cameraPosition);
}
If you are using DefaultClusterRendeder
, create a class like:
public class MyClusterRenderer extends DefaultClusterRenderer implements OnCameraChangeListener {
For those struggling on the above pseudo code by TreKing, you can use the code snippet below
public class MultiListener implements GoogleMap.OnCameraChangeListener {
private List<GoogleMap.OnCameraChangeListener> mListeners = new ArrayList<GoogleMap.OnCameraChangeListener>();
public void registerListener (GoogleMap.OnCameraChangeListener listener) {
mListeners.add(listener);
}
@Override
public void onCameraChange(CameraPosition cameraPosition)
{
for (GoogleMap.OnCameraChangeListener ccl: mListeners)
{
ccl.onCameraChange(cameraPosition);
}
}
}
and to add to MultiListener
MultiListener ml = new MultiListener();
ml.registerListener(mClusterManager);
ml.registerListener(new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition cameraPosition) {
// code here
}
}
);
map.setOnCameraChangeListener(ml);
include cluster manager onCameraChange in your custom camera change listener
mMap.setOnCameraChangeListener(new OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition cameraPosition) {
mClusterManager.onCameraChange(cameraPosition);
.....
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With