Brain Speller
Brain Speller

Reputation: 111

Delete old map markers and load new ones in cluster manager

I have a google map, and what I'm trying to do is to refresh markers from firebase every 15 seconds using geoQuery, And that means I delete all the previous markers and add new ones by using this method :

private void downloadExtraPrinters(LatLng viewCenter, int viewRadius) {

    try {

        final GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(viewCenter.latitude,viewCenter.longitude), viewRadius);
        geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
            @Override
            public void onKeyEntered(String key, GeoLocation location) {

                try{
                    MyItem myItem = new MyItem(location.latitude, location.longitude);
                    if (!items.contains(myItem)) {
                        items.add(myItem);
                    }

                }catch (ClassCastException e){

                }

            }

            @Override
            public void onKeyExited(String key) {

            }

            @Override
            public void onKeyMoved(String key, GeoLocation location) {

            }

            @Override
            public void onGeoQueryReady() {

                parseJsonToList();
            }

            @Override
            public void onGeoQueryError(DatabaseError error) {

            }
        });
    }catch (NullPointerException e){

    }


}

parseJsonToList methode :

private void parseJsonToList() {


itemss = clusterManagerAlgorithm.getItems();
try {
    items.removeAll(itemss);
}catch (IndexOutOfBoundsException e){
    Log.d("itemsDoesn't exist"," : ");
}


mClusterManager.clearItems();      // I Think the problem is in this 3 lines of code.
mClusterManager.cluster();         // I also tried addItems(items) before clustering i still getting the same problem.
mClusterManager.addItems(items);

}

My problem is that every time I want to clean and reload data, all the markers disappear for 15 seconds until the next call of the parseJsonToList method all markers appears again after 15 seconds.

@Override
protected void onStart() {
    super.onStart();

    timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            Log.d("timer","executed");
             if (viewCenter != null) {
                downloadExtraPrinters(viewCenter, viewRadius);
                Log.d("download","newData");
            }

        }
    }, 0, 15000);  // all data reloaded every 15s.
}

Here it is myItem class :

public class MyItem implements ClusterItem {
private final LatLng mPosition;

public MyItem(double lat, double lng) {
    mPosition = new LatLng(lat, lng);
}

@Override
public LatLng getPosition() {
    return mPosition;
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    MyItem item = (MyItem) o;
    return Objects.equals(mPosition, item.mPosition);
}

@Override
public int hashCode() {

    return Objects.hash(mPosition);
}
}

What I want is to change markers immediately without been disappear for 15 seconds. I was struggling with this issue for 2 weeks please help!

Upvotes: 9

Views: 1896

Answers (1)

user2711811
user2711811

Reputation:

Assume the clusterManager is empty initially and the initial geoquery returns a single result A.

On entry to parseJsonToList, items has one entry in it (A) and this line:

itemss = clusterManagerAlgorithm.getItems();

returns an empty Collection.

Since itemss is empty then

items.removeAll(itemss);

has no effect and the rest of the code will add A to the map.

Now, 15 seconds later the same result is returned (A), this line

itemss = clusterManagerAlgorithm.getItems();

returns a collection with one item that is equivalent to A (by position (equals)) and now

items.removeAll(itemss);

will remove the current result (A) because it is also in itemss (again by equals using position).

So then when you add items in this line:

mClusterManager.addItems(items);

nothing is added because items is empty.

And so the pattern is most likely..

the marker appears
15 seconds later the marker disappears
15 seconds later the marker appears

I can't recommend a solution since I can't understand why the clearItems at the end - if you really want to do that then just get rid of the "remove" logic altogether. And the documentation does recommend force cluster after the add.

Upvotes: 2

Related Questions