问题
I'm developing an android application and I'm using Google Maps Android API Utility Library.
To be more specific I'm using the cluster part of that library. Now to my question:
I'm interested in the markers that are shown on the map but that are currently not clustered. I've tried different stuff but can't wrap my head around it. The thing that kind of works is to save all the rendered marker items in a arraylist then compare the marker positions to LatLng bounds of the map on screen, if the markers are inside the screen bounds then the markers info will be added in a different arraylist to later be shown in a listview. The problem with that is when I zoom out, the markers are still in the renderedPersonItems array and will show up in the listview even if those markers are now clustered. I can not clear renderedPersonItems array cause the markers only render once in a lifecycle. So I'm looking for a different solution. This is what I got so far:
Code in DefaultClusterRenderer:
@Override
protected void onBeforeClusterItemRendered(PersonItem personItem, MarkerOptions markerOptions){
markerOptions.title(personItem.personName);
if(!renderedPersonItems.contains(personItem)){
renderedPersonItems.add(personItem);
}
}
Code in main activity:
LatLngBounds personOnScreenBounds = googleMap.getProjection().getVisibleRegion().latLngBounds;
for(PersonItem person : clusterPersonRenderer.getRenderedPersonItems()){
if(PersonsOnScreenBounds.contains(person.getPosition())){
personMarksOnScreen.add(getPersonObjectWithId(person.personId));
}
}
Thanks in advance!
回答1:
I can recommend you another cool android-map library. Android Maps Extensions https://github.com/mg6maciej/android-maps-extensions
I use it in my app and it looks fine. It could be not easy to reimplement it in your app, but you could try.
To solve your problem you can make something like this:
List<Marker> markers = mMap.getDisplayedMarkers();
for (Marker marker : markers) {
if(!marker.isCluster()) {
//
}
}
回答2:
If you want to perform different tasks you might want to make different onClick events on clustered and single markers:
This method will handle clicks on clustered items
public boolean onClusterClick(Cluster<Person> cluster) {
// Show a toast with some info when the cluster is clicked.
Toast.makeText(getActivity(), cluster.getSize() + " (including " + itemText + ")", Toast.LENGTH_SHORT).show();
return true;
}
And this method will handle onClick events if marker is not clustered (has single item)
public boolean onClusterItemClick(Person item) {
// Handles events for single markers
String text = item.text;
return true
}
来源:https://stackoverflow.com/questions/31748401/android-get-the-markers-that-are-not-clustered