Marker Clustering Android
Google Map Clustering Android :-
Overview
This example shows you how to use marker clusters to display a
large number of markers on a map. The marker clustering is used when you
have the large amount of data and you need to show it to the user.
The google marker clustering
provides the better solution to show the large amount of data on the
map.Depending upon the different zoom level the individual marker is shown to
the user.
Add the following dependency to your app's
gradle build file:
These library provides you re-clustering features and animation.
compile
'com.google.maps.android:android-maps-utils:0.5+'
Step 1) You need to
create your own model class and implement
the interface ClusterItem .
Cluster Item interface Override different method .
Cluster Item interface Override different method .
1.
public LatLng
getPosition() :- To set the position
of the marker on the map.
2.
public String getTitle() :-
To set the title of the marker.
3.
public String
getSnippet() :- To set the snippet of the marker.
public class SLocationInfo
implements ClusterItem, Serializable {
private double
sLongitude;
private String sCity;
private double
sLatitude;
public void setSLongitude(double sLongitude)
{
this.sLongitude =
sLongitude;
}
public double
getSLongitude() {
return sLongitude;
}
public void
setSCity(String sCity) {
this.sCity =
sCity;
}
public String
getSCity() {
return sCity;
}
public void
setSLatitude(double sLatitude) {
this.sLatitude =
sLatitude;
}
public double
getSLatitude() {
return sLatitude;
}
@Override
public String toString()
{
return
"SLocationInfo{" +
"sLongitude = '" + sLongitude + '\'' +
",sCity = '" + sCity + '\'' +
",sLatitude = '" + sLatitude + '\'' +
"}";
}
@Override
public LatLng
getPosition() {
return new
LatLng(sLatitude, sLongitude);
}
@Override
public String
getTitle() {
return getSCity();
}
@Override
public String
getSnippet() {
return null;
}
}
Step 2) You need the
configure the cluster manager for your application .
public class MyMapActivity{
private
ClusterManager<SLocationInfo> mClusterManager;
private List<SLocationInfo> mMedia;
@Override
protected void onMapReadyToUse(GoogleMap
mGoogleMap) {
mGoogleMap.clear();
mClusterManager = new
ClusterManager<>(this, mGoogleMap);
mGoogleMap.setOnCameraIdleListener(mClusterManager);
mClusterManager.setAnimation(false);
addAllPin();
}
private void addAllPin() {
mGoogleMap.clear();
mClusterManager.clearItems();
for (int i = 0; i <
mMedia.getMedias().size(); i++) {
mMedia.getMedias().get(i));
mClusterManager.addItem(mMedia.getMedias().get(i).getSMLocationInfo());
// Add items to cluster manager
}
mClusterManager.cluster();
}
}
Step 3) Get the cluster
click and cluster item click .
You need to implement the two different method to get the cluster and
cluster item click.
mClusterManager.setOnClusterClickListener(this);
// For
Cluster Click
mClusterManager.setOnClusterItemClickListener(this); // For Cluster Item
Click
public class MyMapActivity implements
ClusterManager.OnClusterClickListener<SLocationInfo>,
ClusterManager.OnClusterItemClickListener<SLocationInfo>{
private
ClusterManager<SLocationInfo> mClusterManager;
@Override
protected void
onMapReadyToUse(GoogleMap mGoogleMap) {
mGoogleMap.clear();
mClusterManager = new
ClusterManager<>(this, mGoogleMap);
mGoogleMap.setOnCameraIdleListener(mClusterManager);
mClusterManager.setAnimation(false);
mClusterManager.setOnClusterClickListener(this);
mClusterManager.setOnClusterItemClickListener(this);
}
@Override
public boolean
onClusterClick(Cluster<SLocationInfo> cluster) {
return false;
}
@Override
public boolean
onClusterItemClick(SLocationInfo sLocationInfo) {
return false;
}
}
Example
Comments
Post a Comment