Code example
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) Create the map
const initialCamera = createMapCameraPosition({
position: createGeoPoint({ latitude: 35.6812, longitude: 139.7671 }),
zoom: 12,
});
const mapViewState = useMapLibreViewState({
mapDesignType: MapLibreDesign.OsmBrightJa,
cameraPosition: initialCamera,
});
// (2) Prepare the data and interaction state
const [opacity, setOpacity] = useState(0.75);
const tileSource = RasterLayerSource.UrlTemplate({
template: 'https://example.com/tiles/{z}/{x}/{y}.png',
tileSize: 256,
});
// (3) Render the map and its overlays
const layer = createRasterLayerState({
tileSource,
opacity,
});
<MapLibreMapView2D state={mapViewState}>
<RasterLayer state={layer} />
</MapLibreMapView2D>
How the code works
Add a tiled raster source through the shared layer state and update its opacity without replacing the map.
The tileSource is a RasterLayerSource.UrlTemplate pointing at a {z}/{x}/{y} tile URL, while opacity is held in React state.
createRasterLayerState wraps both values, and changing opacity updates the existing RasterLayer in place instead of rebuilding the map.