MapConductor React SDK Samples

แผนที่ร้านค้า | HERE | MapConductor React SDK

ตัวอย่าง แผนที่ร้านค้า ของ MapConductor React SDK ที่ใช้ HERE

ตัวอย่างเชิงโต้ตอบนี้แสดง แผนที่ร้านค้า ผ่านการห่อหุ้มของ MapConductor บน HERE

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

import { useMemo, useState } from 'react';
import {
  createGeoPoint,
  createMapCameraPosition,
  createMarkerState,
  type MarkerState,
} from '@mapconductor/js-sdk-core';
import { InfoBubble, Markers } from '@mapconductor/js-sdk-react';
import { HereMapDesign, HereMapView2D, useHereViewState } from '@mapconductor/react-for-here';


// Before mounting App, add these tags to index.html:
// <script src="https://js.api.here.com/v3/3.2/mapsjs-core.js"></script>
// <script src="https://js.api.here.com/v3/3.2/mapsjs-service.js"></script>
// <script src="https://js.api.here.com/v3/3.2/mapsjs-ui.js"></script>
// <script src="https://js.api.here.com/v3/3.2/mapsjs-mapevents.js"></script>
// <link rel="stylesheet" href="https://js.api.here.com/v3/3.2/mapsjs-ui.css" />


function StoreInfoView({ store }: { store: { name: string; address: string } }) {
  return <article><h3>{store.name}</h3><p>{store.address}</p></article>;
}


export default function App() {
  // (1) สร้างแผนที่


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


  const mapViewState = useHereViewState({
    mapDesignType: HereMapDesign.NormalDay,
    cameraPosition: initialCamera,
  });
  const platform = useMemo(
    () => new H.service.Platform({ apikey: 'YOUR_HERE_API_KEY' }),
    [],
  );


  // (2) เตรียมข้อมูลและสถานะการโต้ตอบ
  const stores = useMemo(() => [
    { id: 'tokyo', name: 'Tokyo Store', address: 'Tokyo Station', lat: 35.6812, lng: 139.7671 },
    { id: 'ginza', name: 'Ginza Store', address: 'Ginza, Tokyo', lat: 35.6717, lng: 139.7650 },
  ], []);
  const [selectedMarker, setSelectedMarker] = useState<MarkerState | null>(null);
  const storeMarkers = useMemo(() => stores.map(store => createMarkerState({
    id: store.id,
    position: createGeoPoint({ latitude: store.lat, longitude: store.lng }),
    extra: store,
    onClick: markerState => setSelectedMarker(markerState),
  })), [stores]);
  const clearSelection = () => setSelectedMarker(null);


  // (3) เรนเดอร์แผนที่และโอเวอร์เลย์


  return (
    <div style={{ height: 500, position: 'relative', display: 'flex', flexDirection: 'column' }}>
      <HereMapView2D platform={platform} state={mapViewState} onMapClick={clearSelection}>
        <Markers states={storeMarkers} />
        {selectedMarker && (
          <InfoBubble marker={selectedMarker}>
            <StoreInfoView store={selectedMarker.extra as (typeof stores)[number]} />
          </InfoBubble>
        )}
      </HereMapView2D>
    </div>
  );
}

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

เรนเดอร์มาร์กเกอร์ร้านค้าทั้งชุดในองค์ประกอบเดียว และแสดงบับเบิลข้อมูลของร้านที่เลือก

อาร์เรย์ storeMarkers สร้างครั้งเดียวด้วย useMemo โดย createMarkerState แต่ละตัวเก็บระเบียนของร้านไว้ในฟิลด์ extra พร้อมตัวจัดการ onClick ที่บันทึก MarkerState ที่ถูกแตะ ส่วน onMapClick จะล้างการเลือก

คอมโพเนนต์ <Markers> แบบรวมกลุ่มวาดทุกร้านในรอบเดียว และ InfoBubble ตรึงอยู่กับมาร์กเกอร์ที่เลือก StoreInfoView จึงปรากฏเฉพาะตอนที่มีร้านถูกเลือกอยู่