Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I make two google maps listeners? setOnCameraChangeListener

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

like image 260
bestKoderEverR Avatar asked Jan 31 '14 14:01

bestKoderEverR


4 Answers

Use the Composite Pattern to create a class that both is a CameraChangeListener and contains CameraChangeListeners, 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.

like image 131
dominicoder Avatar answered Dec 05 '22 02:12

dominicoder


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 {
like image 43
MaciejGórski Avatar answered Dec 05 '22 02:12

MaciejGórski


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);
like image 29
Bih Cheng Avatar answered Dec 05 '22 02:12

Bih Cheng


include cluster manager onCameraChange in your custom camera change listener

mMap.setOnCameraChangeListener(new OnCameraChangeListener() {
    @Override
    public void onCameraChange(CameraPosition cameraPosition) {
        mClusterManager.onCameraChange(cameraPosition);
        .....
    }
});
like image 43
rajeswari ratala Avatar answered Dec 05 '22 02:12

rajeswari ratala