ตัวอย่างโค้ด
import { useState } from 'react';
import {
RasterLayerSource,
createGeoPoint,
createMapCameraPosition,
createRasterLayerState,
} from '@mapconductor/js-sdk-core';
import { RasterLayer } from '@mapconductor/js-sdk-react';
import { MapLibreDesign, MapLibreMapView2D, useMapLibreViewState } from '@mapconductor/react-for-maplibre';
// (1) สร้างแผนที่
const initialCamera = createMapCameraPosition({
position: createGeoPoint({ latitude: 35.6812, longitude: 139.7671 }),
zoom: 12,
});
const mapViewState = useMapLibreViewState({
mapDesignType: MapLibreDesign.OsmBrightJa,
cameraPosition: initialCamera,
});
// (2) เตรียมข้อมูลและสถานะการโต้ตอบ
const [opacity, setOpacity] = useState(0.75);
const tileSource = RasterLayerSource.UrlTemplate({
template: 'https://example.com/tiles/{z}/{x}/{y}.png',
tileSize: 256,
});
// (3) เรนเดอร์แผนที่และโอเวอร์เลย์
const layer = createRasterLayerState({
tileSource,
opacity,
});
<MapLibreMapView2D state={mapViewState}>
<RasterLayer state={layer} />
</MapLibreMapView2D>
โค้ดนี้ทำงานอย่างไร
เพิ่มแหล่งข้อมูลราสเตอร์แบบไทล์ผ่านสถานะเลเยอร์ร่วม และปรับความทึบได้โดยไม่ต้องเปลี่ยนแผนที่
tileSource คือ RasterLayerSource.UrlTemplate ที่ชี้ไปยัง URL ของไทล์แบบ {z}/{x}/{y} ส่วนความทึบเก็บอยู่ในสถานะของ React
createRasterLayerState ห่อค่าทั้งสองไว้ และการเปลี่ยนความทึบจะอัปเดต RasterLayer ตัวเดิมในที่ แทนที่จะสร้างแผนที่ใหม่