Search code examples
androidgoogle-mapsgoogle-maps-markersmarkerclusterer

Clusters don't show when i start the app on googlemapsv2


I have a map and i have a database with json query. I'm trying to create a map where you have markers(title etc.). Because there are so many of them i thought of clustering, so i implemented them. But the problem is that it doesn't work as i want it to. When the app starts, it shows the map, zooms in on your location, but doesn't shows you any markers or clusters. But at the moment you zoom out, the clusters shows, with markers etc. Any ideas what is wrong?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context  = getApplicationContext();
    if (getSharedPreferencesRadius() == null) {
        SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE).edit();
        editor.putInt("radius", 2000);
        editor.commit();
    }
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);



    setUpMapIfNeeded();

}   




@Override
public void onMapReady(GoogleMap googleMap) {
   this.mMap = googleMap;
    setUpMap();

}


@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}


private void setUpMapIfNeeded() {

    if (mMap == null) {

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        Log.d("MAP", " "+mMap);
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();
        }
    }
} 

private void setUpMap() {
    mMap.setMyLocationEnabled(true);
    setMyLocationLatLng();
    kompasEnabled();
    radiusEnable(latLng);
    ZoomIN(latLng);


    mClusterManager = new ClusterManager<Marker>(this, mMap);

    mClusterManager.clearItems();

    mMap.setOnCameraChangeListener(mClusterManager);
    mMap.setOnMarkerClickListener(mClusterManager);

    mClusterManager.cluster();

    jsonRequest(latitude, longitude);



}   

protected GoogleMap getMap() {
    setUpMapIfNeeded();
    return mMap;
}

And this is activity_main

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
   android:id="@+id/container_toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"></include>
    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/clayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/map"
                tools:context=".MainActivity"
         android:name="com.google.android.gms.maps.SupportMapFragment" />

     </android.support.design.widget.CoordinatorLayout>

  </LinearLayout>

If you need any more info, please ask.

UPDATE 1: I found out that without mMap.setOnCameraChangeListener(mClusterManager); It doesn't work(won't even show them). Any ideas?


Solution

  • I found the solution, after hours and hours of looking at my code, and searching the internet i found, that you need when you create all the Items, you need to call:

    mClusterManager.cluster();
    

    That is it.