# How I keep a map smooth with 100,000 points

At work I build map widgets for dashboards: point maps, heatmaps and clustered markers on Google Maps. Some maps have to show a lot of points. Here is the simple way I keep them fast.

## The problem

Say you have **100,000 GPS points** to show on a map.

If you draw every point:

- the browser has to handle 100,000 things, so the map gets slow when you move it;
- and you can't read it anyway: zoomed out, it's just one big blob of dots.

## The fix in one line

**Don't draw every point. Group nearby points, draw only what's on the screen, and let the graphics card do the drawing.**

That's 3 steps.

## Step 1: Group nearby points (once)

Points that are close together become one **bubble** with a number on it, like "38k".

A small library called [Supercluster](https://github.com/mapbox/supercluster) does this. You give it all your points **once**, when the data loads:

```ts
import Supercluster from "supercluster";

const index = new Supercluster({ radius: 60, maxZoom: 16 });

index.load(
  points.map((p) => ({
    type: "Feature" as const,
    properties: {},
    geometry: { type: "Point" as const, coordinates: [p.lng, p.lat] },
  })),
);
```

For 100,000 points this took about **0.23 seconds** on my laptop. Do it once, not every time the map moves. (In React, keep it in `useMemo`.)

## Step 2: Draw only what's on the screen

When the user **stops moving** the map, ask Supercluster: *"What's inside this view, at this zoom?"*

```ts
map.addListener("idle", () => {
  const box = map.getBounds();
  if (!box) return;
  const sw = box.getSouthWest();
  const ne = box.getNorthEast();

  const visible = index.getClusters(
    [sw.lng(), sw.lat(), ne.lng(), ne.lat()], // west, south, east, north
    Math.round(map.getZoom() ?? 5),
  );

  draw(visible);
});
```

This is the magic part. Out of 100,000 points, here is how much actually gets drawn:

| Map view | Drawn on screen |
| --- | --- |
| All of India | 7 bubbles |
| Delhi NCR | 234 |
| Central Delhi | 184 |
| Street level | 37 |

So you draw **a few hundred things at most, never 100,000**. And each answer takes less than 0.04 milliseconds.

## Step 3: Let the graphics card draw

[deck.gl](https://deck.gl) draws using the graphics card (WebGL), on top of the normal Google map. That keeps moving and zooming smooth:

```ts
import { GoogleMapsOverlay } from "@deck.gl/google-maps";
import { ScatterplotLayer } from "@deck.gl/layers";

const overlay = new GoogleMapsOverlay({ layers: [] });
overlay.setMap(map);

function draw(visible: ReturnType<typeof index.getClusters>) {
  overlay.setProps({
    layers: [
      new ScatterplotLayer({
        id: "dots",
        data: visible,
        getPosition: (d) => d.geometry.coordinates as [number, number],
        // Bigger bubble = more points inside it.
        getRadius: (d) => (d.properties.cluster ? 10 + Math.sqrt(d.properties.point_count) / 2 : 4),
        radiusUnits: "pixels",
        getFillColor: [45, 212, 191],
      }),
    ],
  });
}
```

**Nice extra:** when someone clicks a bubble, `index.getClusterExpansionZoom(clusterId)` tells you how far to zoom in so that bubble splits into smaller ones.

## Bonus: don't pay Google twice for the same address

Turning a GPS point into an address ("near Connaught Place") is a paid Google call. Field data visits the same places again and again, so remember what you already asked:

```ts
const saved = new Map<string, Promise<string>>();

function getAddress(lat: number, lng: number) {
  const key = `${lat.toFixed(4)},${lng.toFixed(4)}`; // points ~11 m apart share one answer
  if (!saved.has(key)) {
    saved.set(key, askGoogle(lat, lng)); // save the request itself, not just the answer
  }
  return saved.get(key)!;
}
```

Because it saves the *request*, 10 table rows asking for the same place at the same time make **1** call, not 10. (I tested this.) In real code, also remove the key if the call fails, so it can try again. And check Google's terms before storing addresses for longer than a session.

## Summary

1. **Group** nearby points into bubbles, once.
2. **Ask only for what's on screen**, when the map stops moving.
3. **Draw with the graphics card** using deck.gl.
4. **Remember addresses** you already paid for.

*How I measured: 100,000 made-up points around eight Indian cities, Supercluster 8 on Node 24, on a laptop with an Intel Core i5-13500H. The code was checked with TypeScript against deck.gl 9.4.*

---

I build the data-heavy parts of SaaS products: dashboard builders, report builders and live maps. More about my work: [kodeshaastra.github.io](https://kodeshaastra.github.io)

