Android Open Source - android-maps-utils Grid Based Algorithm From Project Back to project page android-maps-utils .
License The source code is released under:
Apache License
If you think the Android project android-maps-utils listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code package com.google.maps.android.clustering.algo;
/ / f r o m w w w . j a v a 2 s . c o m
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import android.support.v4.util.LongSparseArray;
import com.google.maps.android.clustering.Cluster;
import com.google.maps.android.clustering.ClusterItem;
import com.google.maps.android.geometry.Point;
import com.google.maps.android.projection.SphericalMercatorProjection;
/**
* Groups markers into a grid.
*/
public class GridBasedAlgorithm<T extends ClusterItem> implements Algorithm<T> {
private static final int GRID_SIZE = 100;
private final Set<T> mItems = Collections.synchronizedSet(new HashSet<T>());
@Override
public void addItem(T item) {
mItems.add(item);
}
@Override
public void addItems(Collection<T> items) {
mItems.addAll(items);
}
@Override
public void clearItems() {
mItems.clear();
}
@Override
public void removeItem(T item) {
mItems.remove(item);
}
@Override
public Set<? extends Cluster<T>> getClusters(double zoom) {
long numCells = (long ) Math.ceil(256 * Math.pow(2, zoom) / GRID_SIZE);
SphericalMercatorProjection proj = new SphericalMercatorProjection(numCells);
HashSet<Cluster<T>> clusters = new HashSet<Cluster<T>>();
LongSparseArray<StaticCluster<T>> sparseArray = new LongSparseArray<StaticCluster<T>>();
synchronized (mItems) {
for (T item : mItems) {
Point p = proj.toPoint(item.getPosition());
long coord = getCoord(numCells, p.x, p.y);
StaticCluster<T> cluster = sparseArray.get(coord);
if (cluster == null) {
cluster = new StaticCluster<T>(proj.toLatLng(new Point(Math.floor(p.x) + .5, Math.floor(p.y) + .5)));
sparseArray.put(coord, cluster);
clusters.add(cluster);
}
cluster.add(item);
}
}
return clusters;
}
@Override
public Collection<T> getItems() {
return mItems;
}
private static long getCoord(long numCells, double x, double y) {
return (long ) (numCells * Math.floor(x) + Math.floor(y));
}
}
Java Source Code List com.google.maps.android.MarkerManager.java com.google.maps.android.MathUtil.java com.google.maps.android.PolyUtil.java com.google.maps.android.SphericalUtil.java com.google.maps.android.clustering.ClusterItem.java com.google.maps.android.clustering.ClusterManager.java com.google.maps.android.clustering.Cluster.java com.google.maps.android.clustering.algo.Algorithm.java com.google.maps.android.clustering.algo.GridBasedAlgorithm.java com.google.maps.android.clustering.algo.NonHierarchicalDistanceBasedAlgorithm.java com.google.maps.android.clustering.algo.PreCachingAlgorithmDecorator.java com.google.maps.android.clustering.algo.StaticCluster.java com.google.maps.android.clustering.view.ClusterRenderer.java com.google.maps.android.clustering.view.DefaultClusterRenderer.java com.google.maps.android.geometry.Bounds.java com.google.maps.android.geometry.Point.java com.google.maps.android.heatmaps.Gradient.java com.google.maps.android.heatmaps.HeatmapTileProvider.java com.google.maps.android.heatmaps.WeightedLatLng.java com.google.maps.android.projection.Point.java com.google.maps.android.projection.SphericalMercatorProjection.java com.google.maps.android.quadtree.PointQuadTree.java com.google.maps.android.ui.BubbleDrawable.java com.google.maps.android.ui.BubbleIconFactory.java com.google.maps.android.ui.IconGenerator.java com.google.maps.android.ui.RotationLayout.java com.google.maps.android.ui.SquareTextView.java com.google.maps.android.utils.demo.BaseDemoActivity.java com.google.maps.android.utils.demo.BigClusteringDemoActivity.java com.google.maps.android.utils.demo.ClusteringDemoActivity.java com.google.maps.android.utils.demo.CustomMarkerClusteringDemoActivity.java com.google.maps.android.utils.demo.DistanceDemoActivity.java com.google.maps.android.utils.demo.HeatmapsDemoActivity.java com.google.maps.android.utils.demo.HeatmapsPlacesDemoActivity.java com.google.maps.android.utils.demo.IconGeneratorDemoActivity.java com.google.maps.android.utils.demo.MainActivity.java com.google.maps.android.utils.demo.MultiDrawable.java com.google.maps.android.utils.demo.MyItemReader.java com.google.maps.android.utils.demo.PolyDecodeDemoActivity.java com.google.maps.android.utils.demo.TileProviderAndProjectionDemo.java com.google.maps.android.utils.demo.model.MyItem.java com.google.maps.android.utils.demo.model.Person.java