package com.lewy.googlemapsutil;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.lewy.googlemapsutil.managers.FastClusterManager;
import com.lewy.googlemapsutil.managers.MyDefaultClusterRenderer;

import org.json.JSONException;

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {

    private FastClusterManager<MapItem> mFastClusterManager;

    private GoogleMap mMap;

    LatLng latLng = new LatLng(54.32, 18.64);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

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

        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));

        mFastClusterManager = new FastClusterManager<>(this, mMap, true);

        mMap.setOnCameraChangeListener(mFastClusterManager);
        try {
            readItems();
        } catch (JSONException e) {
            Toast.makeText(this, "Problem reading list of markers.", Toast.LENGTH_LONG).show();
        }

        setSwitchAction();
    }

    private void setSwitchAction() {
        Switch switchView = (Switch) findViewById(R.id.faster_cluster_switch);
        switchView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mFastClusterManager.setRenderer(new MyDefaultClusterRenderer(MainActivity.this, mMap, mFastClusterManager, true));
                } else {
                    mFastClusterManager.setRenderer(new MyDefaultClusterRenderer(MainActivity.this, mMap, mFastClusterManager, false));
                }
            }
        });
    }

    private void readItems() throws JSONException {
        for (int i = 0; i < 100; i++) {
            for (int j = 0; j < 100; j++) {
                LatLng newLatLng = new LatLng(latLng.latitude + ((double) i) / 1000, latLng.longitude + ((double) j) / 1000);
                MapItem mapItem = new MapItem(newLatLng.latitude, newLatLng.longitude);
                mFastClusterManager.addItem(mapItem);
            }
        }
    }
}

@@ -27,16 +27,45 @@
 sutil;%0A%0A
+import android.os.AsyncTask;%0A
 import a
@@ -244,37 +244,8 @@
 tch;
-%0Aimport android.widget.Toast;
 %0A%0Aim
@@ -634,40 +634,8 @@
 r;%0A%0A
-import org.json.JSONException;%0A%0A
 publ
@@ -806,24 +806,32 @@
  mMap;%0A%0A    
+private 
 LatLng latLn
@@ -859,16 +859,61 @@
  18.64);
+ // GDA%C5%83SK%0A%0A    private boolean mManyMarkers;
 %0A%0A    @O
@@ -1641,188 +1641,47 @@
 r);%0A
-        try %7B%0A            readItems();%0A        %7D catch (JSONException e) %7B%0A            Toast.makeText(this, %22Problem reading list of markers.%22, Toast.LENGTH_LONG).show();%0A        %7D
+%0A        new CreateMarkers().execute();
 %0A%0A  
@@ -2030,12 +2030,23 @@
     
-if (
+mManyMarkers = 
 isCh
@@ -2046,32 +2046,26 @@
  = isChecked
-) %7B%0A    
+;%0A
             
@@ -2072,120 +2072,36 @@
     
-mFastClusterManager.setRenderer(new MyDefaultClusterRenderer(MainActivity.this, mMap, mFastClusterManager, true)
+new CreateMarkers().execute(
 );%0A 
@@ -2115,20 +2115,9 @@
     
-    %7D else %7B
+%7D
 %0A   
@@ -2125,239 +2125,203 @@
     
+%7D);%0A
     
+%7D%0A%0A
     
-    mFastClus
+class Crea
 te
-r
 Ma
-nager.setRenderer(new MyDefaultClusterRenderer(MainActivity.this, mMap, mFastClusterManager, false));%0A                %7D%0A            %7D%0A        %7D);%0A    %7D%0A%0A    private void read
+rkers extends AsyncTask%3CVoid, Void, Void%3E %7B%0A        @Override%0A        protected Void doInBackground(Void... arg0) %7B%0A            mFastClusterManager.clear
 Items()
- throws JSONException %7B%0A
+;%0A    
     
@@ -2360,32 +2360,36 @@
 ) %7B%0A            
+    
 for (int j = 0; 
@@ -2400,24 +2400,28 @@
 100; j++) %7B%0A
+    
             
@@ -2550,16 +2550,20 @@
         
+    
 MapItem 
@@ -2634,32 +2634,36 @@
                 
+    
 mFastClusterMana
@@ -2684,16 +2684,20 @@
 pItem);%0A
+    
         
@@ -2694,32 +2694,401 @@
               %7D%0A
+            %7D%0A            return null;%0A        %7D%0A%0A        @Override%0A        protected void onPreExecute() %7B%0A            mFastClusterManager.clearItems();%0A        %7D%0A%0A        @Override%0A        protected void onPostExecute(Void aVoid) %7B%0A            mFastClusterManager.setRenderer(new MyDefaultClusterRenderer(MainActivity.this, mMap, mFastClusterManager, mManyMarkers));%0A
         %7D%0A    %7D%0A

Move add markers (10 000) to async task