Android
Mapview
Markers
Clustering
Mobile Development

Android Mapview Merging overlapping markers into a new marker

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When several markers overlap on an Android map, treating them as one permanent marker is usually the wrong model. The real requirement is normally clustering: group nearby items at low zoom, reveal individual items at high zoom, and keep the original data available for taps, navigation, and details.

Prefer Clustering Over Manual Marker Fusion

A hand-built “merged marker” sounds simple at first, but it creates several problems immediately. Which title should it show, which snippet should win, and when should it split back into separate points. A static merged marker also throws away important information about the underlying items.

The common solution with Google Maps on Android is the Maps Utils clustering library. It handles grouping based on the current zoom level and keeps each item available as the user zooms in.

If your real goal is map readability, clustering is the correct abstraction. Manual coordinate fusion is only appropriate when the data is actually being aggregated permanently, not when the map is only crowded.

Set Up a ClusterManager

Start by representing each map item as a ClusterItem.

java
1import com.google.android.gms.maps.model.LatLng;
2import com.google.maps.android.clustering.ClusterItem;
3
4public class PlaceItem implements ClusterItem {
5    private final LatLng position;
6    private final String title;
7    private final String snippet;
8
9    public PlaceItem(double lat, double lng, String title, String snippet) {
10        this.position = new LatLng(lat, lng);
11        this.title = title;
12        this.snippet = snippet;
13    }
14
15    @Override
16    public LatLng getPosition() {
17        return position;
18    }
19
20    @Override
21    public String getTitle() {
22        return title;
23    }
24
25    @Override
26    public String getSnippet() {
27        return snippet;
28    }
29}

Then attach a ClusterManager to your GoogleMap instance.

java
1import com.google.android.gms.maps.GoogleMap;
2import com.google.maps.android.clustering.ClusterManager;
3
4private ClusterManager<PlaceItem> clusterManager;
5
6private void setUpClusters(GoogleMap map) {
7    clusterManager = new ClusterManager<>(this, map);
8    map.setOnCameraIdleListener(clusterManager);
9    map.setOnMarkerClickListener(clusterManager);
10
11    clusterManager.addItem(new PlaceItem(43.6532, -79.3832, "Office", "Main location"));
12    clusterManager.addItem(new PlaceItem(43.6533, -79.3831, "Cafe", "Nearby stop"));
13    clusterManager.addItem(new PlaceItem(43.6534, -79.3830, "Hotel", "Walking distance"));
14
15    clusterManager.cluster();
16}

That is enough to replace a pile of overlapping markers with zoom-aware cluster markers.

Keep the Original Items and Render Aggregates Visually

A good cluster marker represents a set of items without deleting those items from your application state. That distinction matters. Users may want to zoom in, open the individual place detail screen, or filter one item out later.

Instead of literally replacing data with a synthetic marker, let the cluster renderer draw an aggregate icon. This gives you the visual effect of “one marker” while keeping the map model correct.

java
1import android.content.Context;
2import com.google.android.gms.maps.GoogleMap;
3import com.google.maps.android.clustering.Cluster;
4import com.google.maps.android.clustering.ClusterManager;
5import com.google.maps.android.clustering.view.DefaultClusterRenderer;
6
7public class PlaceRenderer extends DefaultClusterRenderer<PlaceItem> {
8    public PlaceRenderer(Context context, GoogleMap map, ClusterManager<PlaceItem> manager) {
9        super(context, map, manager);
10    }
11
12    @Override
13    protected boolean shouldRenderAsCluster(Cluster<PlaceItem> cluster) {
14        return cluster.getSize() > 1;
15    }
16}

Then install the renderer after creating the manager.

java
clusterManager.setRenderer(new PlaceRenderer(this, map, clusterManager));

Now the cluster appears as one marker when there is crowding, but the individual items still exist and can reappear naturally.

Decide What Happens on Cluster Tap

A useful cluster interaction is usually one of two behaviors: zoom in to reveal the members or show a bottom sheet summarizing the grouped items. Both are better than forcing one marker info window to pretend it represents a single location.

java
1clusterManager.setOnClusterClickListener(cluster -> {
2    LatLng position = cluster.getPosition();
3    map.animateCamera(
4        com.google.android.gms.maps.CameraUpdateFactory.newLatLngZoom(position, map.getCameraPosition().zoom + 2)
5    );
6    return true;
7});

This interaction teaches the user that the marker is a group, not a single place.

When Permanent Merging Actually Makes Sense

There are cases where aggregation is real. For example, if you want one marker that shows “27 stores in this district” and there is no need to reveal individual stores on the map, then a truly merged marker can be valid. In that case, aggregate the data in your backend or view model first and add one marker intentionally.

That is a different product decision from clustering. Do not confuse a presentation problem with a data-model problem.

Common Pitfalls

  • Replacing nearby markers with one permanent marker when the real need is zoom-aware clustering.
  • Losing access to the underlying items after creating a synthetic merged marker.
  • Forgetting to connect ClusterManager to camera idle and marker click listeners.
  • Treating geographically close points and visually overlapping points as identical without testing different zoom levels.
  • Customizing icons before confirming the clustering behavior is correct.

Summary

  • Overlapping map markers are usually a clustering problem, not a merging problem.
  • 'ClusterManager gives you zoom-aware grouping while preserving the original items.'
  • A custom renderer is the right place to change the cluster marker appearance.
  • Cluster taps should zoom or summarize grouped data, not pretend the cluster is one location.
  • Use permanent data aggregation only when your product really needs one combined marker.

Course illustration
Course illustration

All Rights Reserved.