Polski

Tutorial · Google Maps

Pins on Google Maps – markers, clusters and info windows

A practical guide to pins in Google Maps and React – Pin variants per status, custom HTML markers, info windows, custom-styled clusters, levels of detail, syncing a list with the map viewport and performance with 1,500 points.

By · · 22 min read

The pin is the most used map element, and also where apps most quickly become slow and unreadable. In this article you'll build a map of 1,500 parcel lockers where the dumpling shop's customers collect their orders, and walk through every decision: how to show a point's status, when to use a plain pin and when a custom HTML marker, how to group points into clusters and how to keep a list in sync with what's visible on the map.

Prerequisites: @vis.gl/react-google-maps basics (APIProvider, Map, AdvancedMarker) from the Google Maps in React guide, React with hooks and TypeScript.

1,500
points on the map – 8 cities, 3 statuses
9
DOM elements at country view – 8 clusters + 1 highlight
15
zoom threshold – HTML markers instead of clusters from here
1
status style table – pins, markers and the list speak the same colour

Three kinds of markers

KindWhenIn this project
AdvancedMarker + Pinthe classic pin with colour, border, glyph and scaleclustered points, hover and selection highlight
AdvancedMarker with an HTML childcontent that doesn't fit a pin: price, count, photo, labelthe "24 free" pill when zoomed in
cluster marker (renderer)a group of points shown as one markera bubble with the point count

The older google.maps.Marker class has been deprecated since February 2024. All three kinds are built on AdvancedMarkerElement, which requires a Map ID.

Step 1. Project and data

Configuration, styles and entry point

package.json
{
  "name": "pierogi-lockers-map",
  "private": true,
  "version": "0.1.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "tsc --noEmit && vite build",
    "preview": "vite preview"
  },
  "dependencies": {
    "@googlemaps/markerclusterer": "^2.6.2",
    "@vis.gl/react-google-maps": "^1.10.2",
    "react": "^19.3.1",
    "react-dom": "^19.3.1"
  },
  "devDependencies": {
    "@types/google.maps": "^3.67.0",
    "@types/react": "^19.3.0",
    "@types/react-dom": "^19.3.0",
    "@vitejs/plugin-react": "^6.3.0",
    "typescript": "~7.0.2",
    "vite": "^8.4.0"
  }
}
tsconfig.json
{
  "compilerOptions": {
    "target": "es2023",
    "lib": ["ES2023", "DOM", "DOM.Iterable"],
    "module": "esnext",
    "moduleResolution": "bundler",
    "types": ["vite/client", "google.maps"],
    "jsx": "react-jsx",
    "noEmit": true,
    "allowImportingTsExtensions": true,
    "verbatimModuleSyntax": true,
    "skipLibCheck": true,
    "noUncheckedIndexedAccess": true
  },
  "include": ["src", "vite.config.ts"]
}
vite.config.ts
import react from "@vitejs/plugin-react";
import { defineConfig } from "vite";

export default defineConfig({
  plugins: [react()],
});
.env.example
# Copy to .env.local. Restrict the key to your domains (HTTP referrers) and to Maps JavaScript API.
VITE_GOOGLE_MAPS_API_KEY=your-browser-key
VITE_GOOGLE_MAPS_MAP_ID=DEMO_MAP_ID
src/env.d.ts
interface ImportMetaEnv {
  readonly VITE_GOOGLE_MAPS_API_KEY?: string;
  readonly VITE_GOOGLE_MAPS_MAP_ID?: string;
}

interface ImportMeta {
  readonly env: ImportMetaEnv;
}
index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Pierogarnia · Pick-up lockers</title>
  </head>
  <body>
    <div id="root"></div>
    <script type="module" src="/src/main.tsx"></script>
  </body>
</html>
src/index.css
* { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, sans-serif; color: #1f2328; }
.layout { display: grid; grid-template-columns: 360px 1fr; height: 100dvh; }
.sidebar { display: grid; grid-template-rows: auto auto auto 1fr; gap: 12px; padding: 16px; border-right: 1px solid #dee2e6; min-height: 0; }
.filters { display: flex; flex-wrap: wrap; gap: 6px; }
.filters label { display: inline-flex; gap: 6px; align-items: center; padding: 4px 10px; border: 1px solid #dee2e6; border-radius: 999px; cursor: pointer; }
.list { list-style: none; margin: 0; padding: 0; overflow-y: auto; display: grid; gap: 6px; align-content: start; }
.list button { width: 100%; display: flex; justify-content: space-between; gap: 8px; padding: 8px 10px; border: 1px solid #dee2e6; border-radius: 8px; background: #fff; text-align: left; font: inherit; cursor: pointer; }
.list button[aria-current="true"] { border-color: #c2255c; background: #fff0f6; }
.muted { color: #6c757d; font-size: 0.875rem; }
.btn { padding: 6px 12px; white-space: nowrap; border-radius: 8px; border: 1px solid #c2255c; background: #fff; color: #c2255c; font: inherit; cursor: pointer; }
.btn-primary { background: #c2255c; color: #fff; }
/* Cluster bubble created by the custom renderer */
.cluster { display: grid; place-items: center; border-radius: 50%; background: #c2255c; color: #fff; font: 600 13px system-ui, sans-serif; border: 3px solid rgba(255, 255, 255, 0.9); box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3); cursor: pointer; }
/* Rich HTML marker shown at street level */
.slot-badge { position: relative; padding: 3px 8px; border-radius: 999px; background: #fff; border: 2px solid currentColor; font: 600 12px system-ui, sans-serif; white-space: nowrap; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25); transition: transform 120ms ease-out; }
.slot-badge::after { content: ""; position: absolute; left: 50%; bottom: -7px; translate: -50% 0; border: 5px solid transparent; border-top-color: currentColor; }
.slot-badge[data-active="true"] { transform: scale(1.15); background: currentColor; }
.slot-badge[data-active="true"] span { color: #fff; }
@media (prefers-reduced-motion: reduce) { .slot-badge { transition: none; } }
@media (max-width: 760px) { .layout { grid-template-columns: 1fr; grid-template-rows: 45dvh 1fr; } }
src/main.tsx
import { APIProvider } from "@vis.gl/react-google-maps";
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import App from "./App.tsx";
import "./index.css";

const container = document.getElementById("root");
if (!container) throw new Error("#root element not found");

createRoot(container).render(
  <StrictMode>
    <APIProvider apiKey={import.meta.env.VITE_GOOGLE_MAPS_API_KEY ?? ""} language="pl" region="PL">
      <App />
    </APIProvider>
  </StrictMode>,
);

The data is 1,500 points with an id, city, coordinates and number of free slots. The status is derived from the slot count, so a contradictory state like "full with 12 free slots" can't be written down. The geometry helpers work on LatLngBoundsLiteral literals and don't need the maps API to be loaded.

Data and geometry

src/data/lockers.ts
export type LockerStatus = "available" | "busy" | "full";

export interface Locker {
  id: string;
  city: string;
  position: google.maps.LatLngLiteral;
  freeSlots: number;
  status: LockerStatus;
}

const cities = [
  { code: "WAW", name: "Warszawa", lat: 52.2297, lng: 21.0122, count: 380 },
  { code: "KRK", name: "Kraków", lat: 50.0647, lng: 19.945, count: 230 },
  { code: "WRO", name: "Wrocław", lat: 51.1079, lng: 17.0385, count: 200 },
  { code: "GDA", name: "Gdańsk", lat: 54.372, lng: 18.638, count: 180 },
  { code: "POZ", name: "Poznań", lat: 52.4064, lng: 16.9252, count: 160 },
  { code: "LDZ", name: "Łódź", lat: 51.7592, lng: 19.456, count: 140 },
  { code: "KAT", name: "Katowice", lat: 50.2649, lng: 19.0238, count: 120 },
  { code: "LUB", name: "Lublin", lat: 51.2465, lng: 22.5684, count: 90 },
];

// Deterministic pseudo-random generator + Box–Muller for a city-like (dense centre) spread.
let seed = 2026;
const rand = () => ((seed = (seed * 16807) % 2147483647) - 1) / 2147483646;
const gauss = () => Math.sqrt(-2 * Math.log(rand() + 1e-9)) * Math.cos(2 * Math.PI * rand());

export const statusOf = (freeSlots: number): LockerStatus => (freeSlots === 0 ? "full" : freeSlots < 5 ? "busy" : "available");

// 1 500 lockers – in a real app: GET /api/lockers returning id, lat, lng, freeSlots only.
export const lockers: Locker[] = cities.flatMap((city) =>
  Array.from({ length: city.count }, (_, i) => {
    const freeSlots = Math.max(0, Math.round(rand() * 26) - 2);
    return {
      id: `${city.code}-${String(i + 1).padStart(4, "0")}`,
      city: city.name,
      position: { lat: city.lat + gauss() * 0.045, lng: city.lng + gauss() * 0.07 },
      freeSlots,
      status: statusOf(freeSlots),
    };
  }),
);
src/lib/geo.ts
export function inBounds(p: google.maps.LatLngLiteral, b: google.maps.LatLngBoundsLiteral): boolean {
  return p.lat <= b.north && p.lat >= b.south && p.lng <= b.east && p.lng >= b.west;
}

// Grow the bounds a little, so markers just outside the edge are ready before the user pans.
export function padBounds(b: google.maps.LatLngBoundsLiteral, ratio = 0.2): google.maps.LatLngBoundsLiteral {
  const dLat = (b.north - b.south) * ratio;
  const dLng = (b.east - b.west) * ratio;
  return { north: b.north + dLat, south: b.south - dLat, east: b.east + dLng, west: b.west - dLng };
}

export function boundsOf(points: google.maps.LatLngLiteral[]): google.maps.LatLngBoundsLiteral | null {
  if (points.length === 0) return null;
  let north = -90, south = 90, east = -180, west = 180;
  for (const p of points) {
    north = Math.max(north, p.lat);
    south = Math.min(south, p.lat);
    east = Math.max(east, p.lng);
    west = Math.min(west, p.lng);
  }
  return { north, south, east, west };
}

In a real app the API should return only what's needed to draw a point (id, coordinates, status). Fetch the description, opening hours and photos only after a click. With 1,500 points the difference between 60 kB and 1.5 MB of JSON is noticeable on a phone.

The rest of this article is for members

Create a free account or sign in to read it all. Still ahead:

  • Step 2. Pins and custom HTML markers
  • Step 3. Custom-styled clusters
  • Step 4. The info window
  • Step 5. Putting the app together: levels of detail and a list from the viewport
  • Performance: what each step bought us
  • Common mistakes
  • Check yourself
  • What next

Create a free account · Sign in