/*
 * Copyright 2013 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.maps.android.utils.demo;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;

import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.maps.android.clustering.ClusterManager;
import com.google.maps.android.utils.demo.model.MyItem;

public class BigClusteringDemoActivity extends BaseDemoActivity {
    private ClusterManager<MyItem> mClusterManager;

    @Override
    protected void startDemo() {
        getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(51.503186, -0.126446), 10));

        mClusterManager = new ClusterManager<MyItem>(this, getMap());

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

    private void readItems() throws JSONException {
        InputStream inputStream = getResources().openRawResource(R.raw.radar_search);
        List<MyItem> items = new MyItemReader().read(inputStream);
        List<MyItem> offsetItems = new ArrayList<>(items.size() * 10);
        for (int i = 0; i < 10; i++) {
            double offset = i / 60d;
            for (MyItem item : items) {
                LatLng position = item.getPosition();
                double lat = position.latitude + offset;
                double lng = position.longitude + offset;
                MyItem offsetItem = new MyItem(lat, lng);
                offsetItems.add(offsetItem);
            }
        }

        mClusterManager.getMarkerCollection().setItems(offsetItems);
    }
}
@@ -713,16 +713,41 @@
 il.List;
+%0Aimport java.util.Random;
 %0A%0Aimport
@@ -772,16 +772,72 @@
 ption;%0A%0A
+import android.view.Menu;%0Aimport android.view.MenuItem;%0A
 import a
@@ -963,16 +963,72 @@
 LatLng;%0A
+import com.google.android.gms.maps.model.VisibleRegion;%0A
 import c
@@ -1196,24 +1196,25 @@
 oActivity %7B%0A
+%0A
     private 
@@ -1254,16 +1254,1543 @@
 nager;%0A%0A
+    private final MenuItem.OnMenuItemClickListener menuAddMoreItemClickListener = new MenuItem.OnMenuItemClickListener() %7B%0A%0A        final Random random = new Random();%0A%0A        @Override%0A        public boolean onMenuItemClick(MenuItem menuItem) %7B%0A            // get a random item within the visible region%0A            final VisibleRegion visibleRegion = getMap().getProjection().getVisibleRegion();%0A%0A            MyItem item;%0A            do %7B%0A                int randidx = random.nextInt(mClusterManager.getMarkerCollection().getItemSize());%0A                item = mClusterManager.getMarkerCollection().getItem(randidx);%0A            %7D while (!visibleRegion.latLngBounds.contains(item.getPosition()));%0A%0A            List%3CMyItem%3E newItems = new ArrayList%3C%3E(10);%0A%0A            for (int i = 1; i %3C= 10; i++) %7B%0A                double offset = i / 600d;%0A%0A                LatLng position = item.getPosition();%0A                double lat = position.latitude + (offset * ((i %25 (random.nextInt(9) + 1) == 0) ? -1 : 1));%0A                double lng = position.longitude + (offset * ((i %25 (random.nextInt(9) + 1) == 0) ? -1 : 1));%0A                MyItem offsetItem = new MyItem(lat, lng);%0A                newItems.add(offsetItem);%0A            %7D%0A%0A            mClusterManager.getMarkerCollection().addAllItems(newItems);%0A            return true;%0A        %7D%0A    %7D;%0A%0A    @Override%0A    public boolean onCreateOptionsMenu(Menu menu) %7B%0A        menu.add(%22Add more%22).setOnMenuItemClickListener(menuAddMoreItemClickListener);%0A        return true;%0A    %7D%0A%0A
     @Ove

add menu item to the 2k demo to allow to add more markers to the map during runtime