Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GoogleMap onCameraIdle event not firing

I'm trying to clustering the markers on google map because there's too many markers on the map (approximately 6000+ markers).

So I googled and found some documentations about marker clustering and I apply it to my source code, but markers are not shown in google map.

Partial source code of FragMap fragment.

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    SupportMapFragment map = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.display_map);
    if (map == null) {
        Log.e(Tag, "Google Map fragment(id: display_map) is null reference!");
        return;
    }

    map.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    if (DataPackage.getEQMarkerManager() == null) {
        if (!DataPackage.initializeEQMarkerManager(getActivity(), googleMap)) {
            Log.w(Tag, "EQMarkerManager initialization failure");
        }
    }
}

and partial source code of EQMarkerManager and it's implements GoogleMap.OnCameraIdleListener interface.

public EQMarkerManager(Context ctx, GoogleMap map) {
    // ...
    clusterManager = new ClusterManager<>(ctx, map);
    clusterManager.setRenderer(new EQRenderer<>(ctx, map, clusterManager));

    map.setOnCameraIdleListener(this);

    clusterManager.addItems(markers);
}

@Override
public void onCameraIdle() {
    Log.d(Tag, "onCameraIdle");
}

but onCameraIdle callback was never called. Why OnCameraIdle event is not firing?

gradle:

compile 'com.google.android.gms:play-services-maps:9.8.0'
compile 'com.google.maps.android:android-maps-utils:0.4.4+'

Tested on Marshmallow device (SDK version 23).

ADDED: What is the meaning of + on gradle compile?

like image 477
SlaneR Avatar asked Nov 26 '16 07:11

SlaneR


2 Answers

The 'onCameraIdle' event was not firing because I called 'setOnCameraIdleListener' on another class.

Source code from my question

FragMap:

@Override
public void onMapReady(GoogleMap googleMap) {
    if (DataPackage.getEQMarkerManager() == null) {
        if (!DataPackage.initializeEQMarkerManager(getActivity(), googleMap)) {
            Log.w(Tag, "EQMarkerManager initialization failure");
        }
    }
}

EQMarkerManager:

public EQMarkerManager(Context ctx, GoogleMap map) {
    // ...
    clusterManager = new ClusterManager<>(ctx, map);
    clusterManager.setRenderer(new EQRenderer<>(ctx, map, clusterManager));

    map.setOnCameraIdleListener(this);

    clusterManager.addItems(markers);
}


initializeEQMarkerManager method does initialize EQMarkerManager class and EQMarkerManager#ctor calls setOnCameraIdleListener. I don't know much about android, so I'm completely sure about those code works. But it wasn't :/

Anyway, I solve this problem by calling setOnCameraIdleListener inside of FragMap.

@Override
public void onMapReady(GoogleMap googleMap) {
    if (DataPackage.getEQMarkerManager() == null) {
        if (!DataPackage.initializeEQMarkerManager(getActivity())) {
            Log.w(Tag, "EQMarkerManager initialization failure");
            return;
        }
    }

    googleMap.setOnCameraIdleListener(DataPackage.getEQMarkerManager());
}
like image 107
SlaneR Avatar answered Oct 25 '22 03:10

SlaneR


The 'onCameraIdle' will not fire because 'setOnCameraIdleListener' is called internally by ClusterManager class . So what we can do is

mmap.setOnCameraIdleListener(this);

When implementing OnCameraIdleListener

    @Override
    public void onCameraIdle() {
        clustermanager.onCameraIdle();
    }
like image 43
Wahdat Jan Avatar answered Oct 25 '22 05:10

Wahdat Jan