forked from googlemaps/android-maps-utils
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored NonHierarchicalDistanceBasedAlgorithm for inheritence.
ScreenBasedAlgorithmAdapter is now derived from NonHierarchicalDistanceBasedAlgorithm Added new ScreenBasedAlgorithm interface. Implementing this interface, Algorithm has map position and can skip clustering if it is not required. Removed usage of instanceof checks in ClusterManager, which broke algorithms logic, in case of decorations. googlemaps#82
- Loading branch information
1 parent
9db3838
commit 8694da3
Showing
7 changed files
with
238 additions
and
245 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
library/src/com/google/maps/android/clustering/algo/NonHierarchicalViewBasedAlgorithm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright 2016 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.clustering.algo; | ||
|
||
import com.google.android.gms.maps.model.CameraPosition; | ||
import com.google.android.gms.maps.model.LatLng; | ||
import com.google.maps.android.clustering.ClusterItem; | ||
import com.google.maps.android.geometry.Bounds; | ||
import com.google.maps.android.projection.Point; | ||
import com.google.maps.android.projection.SphericalMercatorProjection; | ||
import com.google.maps.android.quadtree.PointQuadTree; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* This algorithm works the same way as {@link NonHierarchicalDistanceBasedAlgorithm} but works, only in | ||
* visible area. It requires to be reclustered on camera movement because clustering is done only for visible area. | ||
* @param <T> | ||
*/ | ||
public class NonHierarchicalViewBasedAlgorithm<T extends ClusterItem> | ||
extends NonHierarchicalDistanceBasedAlgorithm<T> implements ScreenBasedAlgorithm<T> { | ||
|
||
private static final SphericalMercatorProjection PROJECTION = new SphericalMercatorProjection(1); | ||
|
||
private int mViewWidth; | ||
private int mViewHeight; | ||
|
||
private LatLng mMapCenter; | ||
|
||
public NonHierarchicalViewBasedAlgorithm(int screenWidth, int screenHeight) { | ||
mViewWidth = screenWidth; | ||
mViewHeight = screenHeight; | ||
} | ||
|
||
@Override | ||
public void onCameraChange(CameraPosition cameraPosition) { | ||
mMapCenter = cameraPosition.target; | ||
} | ||
|
||
@Override | ||
protected Collection<QuadItem<T>> getClusteringItems(PointQuadTree<QuadItem<T>> quadTree, int discreteZoom) { | ||
return quadTree.search(getVisibleBounds(discreteZoom)); | ||
} | ||
|
||
@Override | ||
public boolean shouldReclusterOnMapMovement() { | ||
return true; | ||
} | ||
|
||
/** | ||
* Update view width and height in case map size was changed. | ||
* You need to recluster all the clusters, to update view state after view size changes. | ||
* @param width map width | ||
* @param height map height | ||
*/ | ||
public void updateViewSize(int width, int height) { | ||
mViewWidth = width; | ||
mViewHeight = height; | ||
} | ||
|
||
private Bounds getVisibleBounds(int zoom) { | ||
if (mMapCenter == null) { | ||
return new Bounds(0, 0, 0, 0); | ||
} | ||
|
||
Point p = PROJECTION.toPoint(mMapCenter); | ||
|
||
final double halfWidthSpan = mViewWidth / Math.pow(2, zoom) / 256 / 2; | ||
final double halfHeightSpan = mViewHeight / Math.pow(2, zoom) / 256 / 2; | ||
|
||
return new Bounds( | ||
p.x - halfWidthSpan, p.x + halfWidthSpan, | ||
p.y - halfHeightSpan, p.y + halfHeightSpan); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
library/src/com/google/maps/android/clustering/algo/ScreenBasedAlgorithm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2016 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.clustering.algo; | ||
|
||
import com.google.android.gms.maps.GoogleMap; | ||
import com.google.maps.android.clustering.ClusterItem; | ||
|
||
/** | ||
* | ||
* This algorithm uses map position for clustering, and should be reclustered on map movement | ||
* @param <T> | ||
*/ | ||
|
||
public interface ScreenBasedAlgorithm<T extends ClusterItem> extends Algorithm<T>, GoogleMap.OnCameraChangeListener { | ||
|
||
boolean shouldReclusterOnMapMovement(); | ||
} |
73 changes: 73 additions & 0 deletions
73
library/src/com/google/maps/android/clustering/algo/ScreenBasedAlgorithmAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2016 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.clustering.algo; | ||
|
||
import com.google.android.gms.maps.model.CameraPosition; | ||
import com.google.maps.android.clustering.Cluster; | ||
import com.google.maps.android.clustering.ClusterItem; | ||
|
||
import java.util.Collection; | ||
import java.util.Set; | ||
|
||
public class ScreenBasedAlgorithmAdapter<T extends ClusterItem> implements ScreenBasedAlgorithm<T> { | ||
|
||
private Algorithm<T> mAlgorithm; | ||
|
||
public ScreenBasedAlgorithmAdapter(Algorithm<T> algorithm) { | ||
mAlgorithm = algorithm; | ||
} | ||
|
||
@Override | ||
public boolean shouldReclusterOnMapMovement() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void addItem(T item) { | ||
mAlgorithm.addItem(item); | ||
} | ||
|
||
@Override | ||
public void addItems(Collection<T> items) { | ||
mAlgorithm.addItems(items); | ||
} | ||
|
||
@Override | ||
public void clearItems() { | ||
mAlgorithm.clearItems(); | ||
} | ||
|
||
@Override | ||
public void removeItem(T item) { | ||
mAlgorithm.removeItem(item); | ||
} | ||
|
||
@Override | ||
public Set<? extends Cluster<T>> getClusters(double zoom) { | ||
return mAlgorithm.getClusters(zoom); | ||
} | ||
|
||
@Override | ||
public Collection<T> getItems() { | ||
return mAlgorithm.getItems(); | ||
} | ||
|
||
@Override | ||
public void onCameraChange(CameraPosition cameraPosition) { | ||
// stub | ||
} | ||
} |
Oops, something went wrong.