MapConductor React SDK Samples

Postfilialen-Clustering | MapLibre | MapConductor React SDK

Postfilialen-Clustering-Beispiel des MapConductor React SDK mit MapLibre.

Dieses interaktive Beispiel zeigt Postfilialen-Clustering über die MapConductor-Abstraktion auf MapLibre.

Codebeispiel

import { useMemo, useState } from 'react';
import {
  ColorDefaultIcon,
  createGeoPoint,
  createGeoRectBounds,
  createMapCameraPosition,
  createMarkerState,
  type MarkerState,
} from '@mapconductor/js-sdk-core';
import { InfoBubble } from '@mapconductor/js-sdk-react';
import { MapLibreDesign, MapLibreMapView2D, useMapLibreViewState } from '@mapconductor/react-for-maplibre';
import { MarkerClusterGroup, type MarkerCluster } from '@mapconductor/react-marker-clustering';
import '@mapconductor/react-for-maplibre/style.css';


export default function App() {
  // (1) Die Karte erzeugen


  const initialCamera = createMapCameraPosition({
    position: createGeoPoint({ latitude: 43.05, longitude: 141.31 }),
    zoom: 11,
  });


  const mapViewState = useMapLibreViewState({
    mapDesignType: MapLibreDesign.OsmBrightJa,
    cameraPosition: initialCamera,
  });


  // (2) Daten und Interaktionszustand vorbereiten
  const postOffices = useMemo(() => [
    { id: 'maruyama', lat: 43.043751, lng: 141.305428, name: '円山西町簡易郵便局' },
    { id: 'jingu', lat: 43.057305, lng: 141.311483, name: '北海道神宮前郵便局' },
    { id: 'miyanomori', lat: 43.057305, lng: 141.303150, name: '札幌宮の森郵便局' },
    { id: 'araiyama', lat: 43.050833, lng: 141.298262, name: '札幌荒井山簡易郵便局' },
    { id: 'futagoyama', lat: 43.043085, lng: 141.318733, name: '札幌双子山郵便局' },
  ], []);
  const [selected, setSelected] = useState<MarkerState | null>(null);
  const postOfficeMarkers = useMemo(() => postOffices.map(office => createMarkerState({
    id: office.id,
    position: createGeoPoint({ latitude: office.lat, longitude: office.lng }),
    extra: office.name,
    onClick: markerState => setSelected(markerState),
  })), [postOffices]);
  const clusterIconProvider = (count: number) => new ColorDefaultIcon({
    fillColor: '#dc2626', label: String(count), labelTextColor: '#ffffff',
  });
  const zoomToCluster = (cluster: MarkerCluster) => {
    const bounds = createGeoRectBounds();
    postOfficeMarkers.filter(marker => cluster.markerIds.includes(marker.id))
      .forEach(marker => bounds.extend(marker.position));
    mapViewState.fitBounds(bounds, 60);
  };


  // (3) Die Karte und ihre Overlays rendern


  return (
    <div style={{ height: 500, position: 'relative', display: 'flex', flexDirection: 'column' }}>
      <MapLibreMapView2D state={mapViewState} onMapClick={() => setSelected(null)}>
        <MarkerClusterGroup
          markers={postOfficeMarkers}
          clusterIconProvider={clusterIconProvider}
          onClusterClick={zoomToCluster}
          minClusterSize={3}
          clusterRadiusPx={80}
        />
        {selected && <InfoBubble marker={selected}>{selected.extra as string}</InfoBubble>}
      </MapLibreMapView2D>
    </div>
  );
}

Wie der Code funktioniert

Eine große Markersammlung clustern und über die Erweiterungs-API ein eigenes Cluster-Icon und Klickverhalten mitgeben.

Dasselbe postOfficeMarkers-Array speist MarkerClusterGroup statt <Markers>, sodass nahe beieinanderliegende Filialen beim Herauszoomen automatisch gruppiert werden.

clusterIconProvider zeichnet das Cluster-Abzeichen, onClusterClick führt zoomToCluster aus, um eine Gruppe zu öffnen, und minClusterSize zusammen mit clusterRadiusPx regelt, wie stark Punkte zusammengefasst werden.