Search in sources :

Example 1 with Cluster

use of com.google.maps.android.clustering.Cluster in project android-maps-utils by googlemaps.

the class GridBasedAlgorithm method getClusters.

@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;
}
Also used : LongSparseArray(android.support.v4.util.LongSparseArray) Cluster(com.google.maps.android.clustering.Cluster) Point(com.google.maps.android.geometry.Point) SphericalMercatorProjection(com.google.maps.android.projection.SphericalMercatorProjection) HashSet(java.util.HashSet)

Example 2 with Cluster

use of com.google.maps.android.clustering.Cluster in project android-maps-utils by googlemaps.

the class NonHierarchicalDistanceBasedAlgorithm method getClusters.

@Override
public Set<? extends Cluster<T>> getClusters(double zoom) {
    final int discreteZoom = (int) zoom;
    final double zoomSpecificSpan = MAX_DISTANCE_AT_ZOOM / Math.pow(2, discreteZoom) / 256;
    final Set<QuadItem<T>> visitedCandidates = new HashSet<QuadItem<T>>();
    final Set<Cluster<T>> results = new HashSet<Cluster<T>>();
    final Map<QuadItem<T>, Double> distanceToCluster = new HashMap<QuadItem<T>, Double>();
    final Map<QuadItem<T>, StaticCluster<T>> itemToCluster = new HashMap<QuadItem<T>, StaticCluster<T>>();
    synchronized (mQuadTree) {
        for (QuadItem<T> candidate : mItems) {
            if (visitedCandidates.contains(candidate)) {
                // Candidate is already part of another cluster.
                continue;
            }
            Bounds searchBounds = createBoundsFromSpan(candidate.getPoint(), zoomSpecificSpan);
            Collection<QuadItem<T>> clusterItems;
            clusterItems = mQuadTree.search(searchBounds);
            if (clusterItems.size() == 1) {
                // Only the current marker is in range. Just add the single item to the results.
                results.add(candidate);
                visitedCandidates.add(candidate);
                distanceToCluster.put(candidate, 0d);
                continue;
            }
            StaticCluster<T> cluster = new StaticCluster<T>(candidate.mClusterItem.getPosition());
            results.add(cluster);
            for (QuadItem<T> clusterItem : clusterItems) {
                Double existingDistance = distanceToCluster.get(clusterItem);
                double distance = distanceSquared(clusterItem.getPoint(), candidate.getPoint());
                if (existingDistance != null) {
                    // Item already belongs to another cluster. Check if it's closer to this cluster.
                    if (existingDistance < distance) {
                        continue;
                    }
                    // Move item to the closer cluster.
                    itemToCluster.get(clusterItem).remove(clusterItem.mClusterItem);
                }
                distanceToCluster.put(clusterItem, distance);
                cluster.add(clusterItem.mClusterItem);
                itemToCluster.put(clusterItem, cluster);
            }
            visitedCandidates.addAll(clusterItems);
        }
    }
    return results;
}
Also used : HashMap(java.util.HashMap) Bounds(com.google.maps.android.geometry.Bounds) Cluster(com.google.maps.android.clustering.Cluster) Point(com.google.maps.android.geometry.Point) HashSet(java.util.HashSet)

Aggregations

Cluster (com.google.maps.android.clustering.Cluster)2 Point (com.google.maps.android.geometry.Point)2 HashSet (java.util.HashSet)2 LongSparseArray (android.support.v4.util.LongSparseArray)1 Bounds (com.google.maps.android.geometry.Bounds)1 SphericalMercatorProjection (com.google.maps.android.projection.SphericalMercatorProjection)1 HashMap (java.util.HashMap)1