MapConductor React SDK Samples

เลเยอร์ฮีตแมป | MapLibre | MapConductor React SDK

ตัวอย่าง เลเยอร์ฮีตแมป ของ MapConductor React SDK ที่ใช้ MapLibre

ตัวอย่างเชิงโต้ตอบนี้แสดง เลเยอร์ฮีตแมป ผ่านการห่อหุ้มของ MapConductor บน MapLibre

ตัวอย่างโค้ด

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) สร้างแผนที่


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


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


  // (2) เตรียมข้อมูลและสถานะการโต้ตอบ
  // 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) เรนเดอร์แผนที่และโอเวอร์เลย์


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

โค้ดนี้ทำงานอย่างไร

HeatmapOverlay แสดงความหนาแน่นของจุดภูมิศาสตร์

useMemo สร้าง HeatmapPointState ตัวอย่าง 100 จุดโดยไม่ใช้ไฟล์ภายนอก และ HeatmapPoints ส่งจุดทั้งหมดให้โอเวอร์เลย์พร้อมกัน