MapConductor React SDK Samples

Capa de mapa de calor | MapLibre | MapConductor React SDK

Ejemplo de Capa de mapa de calor de MapConductor React SDK con MapLibre.

Este ejemplo interactivo muestra Capa de mapa de calor mediante la abstracción de MapConductor sobre MapLibre.

Ejemplo de código

import { useMemo } from 'react';
import { createGeoPoint, createMapCameraPosition } from '@mapconductor/js-sdk-core';
import { MapLibreDesign, MapLibreMapView2D, useMapLibreViewState } from '@mapconductor/react-for-maplibre';
import { HeatmapOverlay, HeatmapPointState, HeatmapPoints } from '@mapconductor/react-heatmap';
import '@mapconductor/react-for-maplibre/style.css';


export default function App() {
  // (1) Crear el mapa


  const initialCamera = createMapCameraPosition({
    position: createGeoPoint({ latitude: 35.6812, longitude: 139.7671 }),
    zoom: 12,
  });


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


  // (2) Preparar los datos y el estado de interacción
  // Inline points work without the example app's public data files.
  const heatmapPoints = useMemo(() => Array.from({ length: 100 }, (_, index) =>
    new HeatmapPointState({
      id: `point-${index}`,
      position: createGeoPoint({
        latitude: 35.68 + Math.sin(index * 2.4) * (index % 10) * 0.001,
        longitude: 139.77 + Math.cos(index * 2.4) * (index % 10) * 0.001,
      }),
    }),
  ), []);


  // (3) Renderizar el mapa y sus capas


  return (
    <div style={{ height: 500, position: 'relative', display: 'flex', flexDirection: 'column' }}>
      <MapLibreMapView2D state={mapViewState}>
        <HeatmapOverlay>
          <HeatmapPoints states={heatmapPoints} />
        </HeatmapOverlay>
      </MapLibreMapView2D>
    </div>
  );
}

Cómo funciona el código

HeatmapOverlay dibuja la densidad de puntos geográficos.

useMemo crea 100 objetos HeatmapPointState de ejemplo sin archivos externos. HeatmapPoints los envía juntos a la capa.