Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.playmoweb:google-maps-clustering:v0.1.4.3'
}
dependencies {
implementation("com.github.playmoweb:google-maps-clustering:v0.1.4.3")
}
<dependency>
<groupId>com.github.playmoweb</groupId>
<artifactId>google-maps-clustering</artifactId>
<version>v0.1.4.3</version>
</dependency>
libraryDependencies += "com.github.playmoweb" % "google-maps-clustering" % "v0.1.4.3"
:dependencies [[com.github.playmoweb/google-maps-clustering "v0.1.4.3"]]
<a href="http://www.methodscount.com/?lib=net.sharewire%3Agoogle-maps-clustering%3A0.1.2-beta"><img src="https://img.shields.io/badge/Methods count-193-e91e63.svg"/></a>
A fast marker clustering library for Google Maps Android API.
Why not use Google Maps Android API Utility Library? Because it's very slow for large amounts of markers, which causes skipping frames and ANRs (see Issue #29, Issue #82). But this library can easily handle thousands of markers (the video above demonstrates the sample application with 20 000 markers running on Nexus 5).
repositories {
jcenter()
}
dependencies {
compile 'net.sharewire:google-maps-clustering:0.1.3'
}
ClusterItem
to represent a marker on the map. The cluster item returns the position of the marker and an optional title or snippet:
class SampleClusterItem implements ClusterItem {
private final LatLng location;
SampleClusterItem(@NonNull LatLng location) {
this.location = location;
}
@Override
public double getLatitude() {
return location.latitude;
}
@Override
public double getLongitude() {
return location.longitude;
}
@Nullable
@Override
public String getTitle() {
return null;
}
@Nullable
@Override
public String getSnippet() {
return null;
}
}
GoogleMap.setOnCameraIdleListener(...)
:ClusterManager<SampleClusterItem> clusterManager = new ClusterManager<>(context, googleMap);
googleMap.setOnCameraIdleListener(clusterManager);
ClusterManager.setCallbacks(...)
:clusterManager.setCallbacks(new ClusterManager.Callbacks<SampleClusterItem>() {
@Override
public boolean onClusterClick(@NonNull Cluster<SampleClusterItem> cluster) {
Log.d(TAG, "onClusterClick");
return false;
}
@Override
public boolean onClusterItemClick(@NonNull SampleClusterItem clusterItem) {
Log.d(TAG, "onClusterItemClick");
return false;
}
});
To customize the icons create an instance of IconGenerator
and set it using ClusterManager.setIconGenerator(...)
. You can also use the default implementation DefaultIconGenerator
and customize the style of icons using DefaultIconGenerator.setIconStyle(...)
.
Populate ClusterManager with items using ClusterManager.setItems(...)
:
List<SampleClusterItem> clusterItems = generateSampleClusterItems();
clusterManager.setItems(clusterItems);