Ejemplo de código
import { useState } from 'react';
import {
createGeoPoint,
createGeoRectBounds,
createGroundImageState,
createMapCameraPosition,
createMarkerState,
type MarkerState,
} from '@mapconductor/js-sdk-core';
import { GroundImage, Markers } from '@mapconductor/js-sdk-react';
import { MapLibreDesign, MapLibreMapView2D, useMapLibreViewState } from '@mapconductor/react-for-maplibre';
// (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
const [groundImageState] = useState(() => createGroundImageState({
id: 'historic-map', imageUrl, bounds: initialBounds, opacity: 0.7,
}));
const moveSouthWest = (markerState: MarkerState) => {
groundImageState.bounds = createGeoRectBounds({
southWest: markerState.position,
northEast: groundImageState.bounds.northEast,
});
};
const southWestMarker = createMarkerState({
id: 'south-west', position: groundImageState.bounds.southWest!,
draggable: true, onDrag: moveSouthWest, onDragEnd: moveSouthWest,
});
const cornerMarkers = [southWestMarker, northEastMarker];
// (3) Renderizar el mapa y sus capas
<MapLibreMapView2D state={mapViewState}>
<GroundImage state={groundImageState} />
<Markers states={cornerMarkers} />
</MapLibreMapView2D>
Cómo funciona el código
Conserva una instancia de GroundImageState y actualiza sus límites desde marcadores de esquina arrastrables.
Se crea un único GroundImageState con useState y nunca se reemplaza; arrastrar una esquina llama a moveSouthWest, que asigna un createGeoRectBounds nuevo a groundImageState.bounds.
Como el estado es observable, cada asignación de límites se envía al proveedor del mapa, por lo que la imagen superpuesta se estira para seguir a los marcadores de esquina.