Skip to content

Commit fc66e48

Browse files
authored
chore: Cleaned up cluster diff demo (#1446)
1 parent 1ba553d commit fc66e48

File tree

1 file changed

+124
-129
lines changed

1 file changed

+124
-129
lines changed

demo/src/main/java/com/google/maps/android/utils/demo/ClusteringDiffDemoActivity.java

Lines changed: 124 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,32 @@
4646
import java.util.ArrayList;
4747
import java.util.List;
4848

49+
enum City {
50+
ENFIELD(new LatLng(51.6524, -0.0838), "Enfield"),
51+
ILFORD(new LatLng(51.5590, -0.0815), "Ilford"),
52+
LONDON(new LatLng(51.5074, -0.1278), "London");
53+
54+
public final LatLng latLng;
55+
public final String label;
56+
57+
City(LatLng latLng, String label) {
58+
this.latLng = latLng;
59+
this.label = label;
60+
}
61+
}
62+
4963
/**
5064
* Demonstrates how to apply a diff to the current Cluster
5165
*/
52-
public class ClusteringDiffDemoActivity extends BaseDemoActivity implements ClusterManager.OnClusterClickListener<Person>, ClusterManager.OnClusterInfoWindowClickListener<Person>, ClusterManager.OnClusterItemClickListener<Person>, ClusterManager.OnClusterItemInfoWindowClickListener<Person> {
53-
private ClusterManager<Person> mClusterManager;
54-
private Person itemtoUpdate = new Person(ENFIELD, "Teach", R.drawable.teacher);
55-
56-
private static final LatLng ENFIELD = new LatLng(51.6524, -0.0838);
57-
private static final LatLng ILFORD = new LatLng(51.5590, -0.0815);
66+
public class ClusteringDiffDemoActivity extends BaseDemoActivity
67+
implements ClusterManager.OnClusterClickListener<Person>,
68+
ClusterManager.OnClusterInfoWindowClickListener<Person>,
69+
ClusterManager.OnClusterItemClickListener<Person>,
70+
ClusterManager.OnClusterItemInfoWindowClickListener<Person> {
5871

59-
private static final LatLng LONDON = new LatLng(51.5074, -0.1278);
60-
LatLng midpoint = getMidpoint();
72+
private final LatLng midpoint = getMidpoint();
73+
private ClusterManager<Person> mClusterManager;
74+
private Person itemToUpdate = new Person(City.ENFIELD.latLng, "Teach", R.drawable.teacher);
6175
private int currentLocationIndex = 0;
6276

6377
protected int getLayoutId() {
@@ -71,11 +85,108 @@ public void onMapReady(@NonNull GoogleMap map) {
7185
getMap().animateCamera(CameraUpdateFactory.newLatLngZoom(midpoint, 12));
7286
}
7387

74-
7588
private LatLng getMidpoint() {
76-
double latitude = (ClusteringDiffDemoActivity.ENFIELD.latitude + ClusteringDiffDemoActivity.ILFORD.latitude + ClusteringDiffDemoActivity.LONDON.latitude) / 3;
77-
double longitude = (ClusteringDiffDemoActivity.ENFIELD.longitude + ClusteringDiffDemoActivity.ILFORD.longitude + ClusteringDiffDemoActivity.LONDON.longitude) / 3;
78-
return new LatLng(latitude, longitude);
89+
double latitude = 0.0;
90+
double longitude = 0.0;
91+
92+
for (City city: City.values()) {
93+
latitude += city.latLng.latitude;
94+
longitude += city.latLng.longitude;
95+
}
96+
97+
int numCities = City.values().length;
98+
99+
return new LatLng(latitude / numCities, longitude / numCities);
100+
}
101+
102+
@Override
103+
public boolean onClusterClick(Cluster<Person> cluster) {
104+
// Show a toast with some info when the cluster is clicked.
105+
String firstName = cluster.getItems().iterator().next().name;
106+
Toast.makeText(this, cluster.getSize() + " (including " + firstName + ")", Toast.LENGTH_SHORT).show();
107+
108+
// Zoom in the cluster. Need to create LatLngBounds and including all the cluster items
109+
// inside of bounds, then animate to center of the bounds.
110+
111+
// Create the builder to collect all essential cluster items for the bounds.
112+
LatLngBounds.Builder builder = LatLngBounds.builder();
113+
for (ClusterItem item : cluster.getItems()) {
114+
builder.include(item.getPosition());
115+
}
116+
// Get the LatLngBounds
117+
final LatLngBounds bounds = builder.build();
118+
119+
// Animate camera to the bounds
120+
try {
121+
getMap().animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 200));
122+
} catch (Exception e) {
123+
e.printStackTrace();
124+
}
125+
126+
return true;
127+
}
128+
129+
@Override
130+
public void onClusterInfoWindowClick(Cluster<Person> cluster) {
131+
// Does nothing, but you could go to a list of the users.
132+
}
133+
134+
@Override
135+
public boolean onClusterItemClick(Person item) {
136+
// Does nothing, but you could go into the user's profile page, for example.
137+
return false;
138+
}
139+
140+
@Override
141+
public void onClusterItemInfoWindowClick(Person item) {
142+
// Does nothing, but you could go into the user's profile page, for example.
143+
}
144+
145+
@Override
146+
protected void startDemo(boolean isRestore) {
147+
if (!isRestore) {
148+
getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(51.503186, -0.126446), 6));
149+
}
150+
151+
mClusterManager = new ClusterManager<>(this, getMap());
152+
mClusterManager.setRenderer(new PersonRenderer());
153+
getMap().setOnCameraIdleListener(mClusterManager);
154+
getMap().setOnMarkerClickListener(mClusterManager);
155+
getMap().setOnInfoWindowClickListener(mClusterManager);
156+
mClusterManager.setOnClusterClickListener(this);
157+
mClusterManager.setOnClusterInfoWindowClickListener(this);
158+
mClusterManager.setOnClusterItemClickListener(this);
159+
mClusterManager.setOnClusterItemInfoWindowClickListener(this);
160+
161+
addItems();
162+
mClusterManager.cluster();
163+
}
164+
165+
private void addItems() {
166+
// Marker in Enfield
167+
mClusterManager.addItem(new Person(City.ENFIELD.latLng, "John", R.drawable.john));
168+
169+
// Marker in the center of London
170+
itemToUpdate = new Person(City.LONDON.latLng, "Teach", R.drawable.teacher);
171+
mClusterManager.addItem(itemToUpdate);
172+
}
173+
174+
private void rotateLocation() {
175+
// Update the current index to cycle through locations (0 = Enfield, 1 = Olford, 2 = London)
176+
currentLocationIndex = (currentLocationIndex + 1) % City.values().length;
177+
178+
City nextCity = City.values()[currentLocationIndex];
179+
180+
LatLng newLocation = nextCity.latLng;
181+
String cityName = nextCity.label;
182+
183+
Log.d("ClusterTest", "Item rotated to: " + newLocation.toString() + ", City: " + cityName);
184+
185+
if (itemToUpdate != null) {
186+
itemToUpdate = new Person(newLocation, "Teach", R.drawable.teacher);
187+
mClusterManager.updateItem(itemToUpdate); // Update the marker
188+
mClusterManager.cluster();
189+
}
79190
}
80191

81192
/**
@@ -108,9 +219,7 @@ public PersonRenderer() {
108219
@Override
109220
protected void onBeforeClusterItemRendered(@NonNull Person person, @NonNull MarkerOptions markerOptions) {
110221
// Draw a single person - show their profile photo and set the info window to show their name
111-
markerOptions
112-
.icon(getItemIcon(person))
113-
.title(person.name);
222+
markerOptions.icon(getItemIcon(person)).title(person.name);
114223
}
115224

116225
@Override
@@ -180,118 +289,4 @@ protected boolean shouldRenderAsCluster(@NonNull Cluster<Person> cluster) {
180289
return cluster.getSize() >= 2;
181290
}
182291
}
183-
184-
185-
@Override
186-
public boolean onClusterClick(Cluster<Person> cluster) {
187-
// Show a toast with some info when the cluster is clicked.
188-
String firstName = cluster.getItems().iterator().next().name;
189-
Toast.makeText(this, cluster.getSize() + " (including " + firstName + ")", Toast.LENGTH_SHORT).show();
190-
191-
// Zoom in the cluster. Need to create LatLngBounds and including all the cluster items
192-
// inside of bounds, then animate to center of the bounds.
193-
194-
// Create the builder to collect all essential cluster items for the bounds.
195-
LatLngBounds.Builder builder = LatLngBounds.builder();
196-
for (ClusterItem item : cluster.getItems()) {
197-
builder.include(item.getPosition());
198-
}
199-
// Get the LatLngBounds
200-
final LatLngBounds bounds = builder.build();
201-
202-
// Animate camera to the bounds
203-
try {
204-
getMap().animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100));
205-
} catch (Exception e) {
206-
e.printStackTrace();
207-
}
208-
209-
return true;
210-
}
211-
212-
@Override
213-
public void onClusterInfoWindowClick(Cluster<Person> cluster) {
214-
// Does nothing, but you could go to a list of the users.
215-
}
216-
217-
@Override
218-
public boolean onClusterItemClick(Person item) {
219-
// Does nothing, but you could go into the user's profile page, for example.
220-
return false;
221-
}
222-
223-
@Override
224-
public void onClusterItemInfoWindowClick(Person item) {
225-
// Does nothing, but you could go into the user's profile page, for example.
226-
}
227-
228-
@Override
229-
protected void startDemo(boolean isRestore) {
230-
if (!isRestore) {
231-
getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(51.503186, -0.126446), 6));
232-
}
233-
234-
mClusterManager = new ClusterManager<>(this, getMap());
235-
mClusterManager.setRenderer(new PersonRenderer());
236-
getMap().setOnCameraIdleListener(mClusterManager);
237-
getMap().setOnMarkerClickListener(mClusterManager);
238-
getMap().setOnInfoWindowClickListener(mClusterManager);
239-
mClusterManager.setOnClusterClickListener(this);
240-
mClusterManager.setOnClusterInfoWindowClickListener(this);
241-
mClusterManager.setOnClusterItemClickListener(this);
242-
mClusterManager.setOnClusterItemInfoWindowClickListener(this);
243-
244-
addItems();
245-
mClusterManager.cluster();
246-
}
247-
248-
private void addItems() {
249-
// Marker in Enfield
250-
mClusterManager.addItem(new Person(ENFIELD, "John", R.drawable.john));
251-
252-
// Marker in the center of London
253-
itemtoUpdate = new Person(LONDON, "Teach", R.drawable.teacher);
254-
mClusterManager.addItem(itemtoUpdate);
255-
}
256-
257-
private void rotateLocation() {
258-
// Update the current index to cycle through locations (0 = Enfield, 1 = Olford, 2 = London)
259-
currentLocationIndex = (currentLocationIndex + 1) % 3;
260-
261-
262-
LatLng newLocation = switch (currentLocationIndex) {
263-
case 0 -> ENFIELD;
264-
case 1 -> ILFORD;
265-
default -> LONDON;
266-
};
267-
268-
String cityName = getCityName(newLocation);
269-
270-
Log.d("ClusterTest", "Item rotated to: " + newLocation.toString() + ", City: " + cityName);
271-
272-
if (itemtoUpdate != null) {
273-
itemtoUpdate = new Person(newLocation, "Teach", R.drawable.teacher);
274-
mClusterManager.updateItem(itemtoUpdate); // Update the marker
275-
mClusterManager.cluster();
276-
}
277-
}
278-
279-
// Method to map LatLng to city name
280-
private String getCityName(LatLng location) {
281-
if (areLocationsEqual(location, ENFIELD)) {
282-
return "Enfield";
283-
} else if (areLocationsEqual(location, ILFORD)) {
284-
return "Ilford";
285-
} else if (areLocationsEqual(location, LONDON)) {
286-
return "London";
287-
} else {
288-
return "Unknown City"; // Default case if location is not recognized
289-
}
290-
}
291-
292-
// Method to compare LatLng objects with a tolerance
293-
private boolean areLocationsEqual(LatLng loc1, LatLng loc2) {
294-
return Math.abs(loc1.latitude - loc2.latitude) < 1E-5 &&
295-
Math.abs(loc1.longitude - loc2.longitude) < 1E-5;
296-
}
297292
}

0 commit comments

Comments
 (0)