Ejemplo de código
import { useMemo } from 'react';
import {
createGeoPoint,
createGeoRectBounds,
createMapCameraPosition,
createMarkerState,
} from '@mapconductor/js-sdk-core';
import { Marker } from '@mapconductor/js-sdk-react';
import { MapLibreDesign, MapLibreMapView2D, useMapLibreViewState } from '@mapconductor/react-for-maplibre';
import '@mapconductor/react-for-maplibre/style.css';
export default function App() {
// (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 bounds = useMemo(() => createGeoRectBounds({
southWest: createGeoPoint({ latitude: 35.66, longitude: 139.74 }),
northEast: createGeoPoint({ latitude: 35.70, longitude: 139.80 }),
}), []);
const marker = useMemo(() => createMarkerState({
position: createGeoPoint({ latitude: 35.68, longitude: 139.76 }),
draggable: true,
onDragStart: state => {
bounds.southWest = state.position;
bounds.northEast = state.position;
},
onDragEnd: state => {
bounds.extend(state.position);
mapViewState.fitBounds(bounds, 40);
},
}), [bounds, mapViewState]);
// (3) Renderizar el mapa y sus capas
return (
<div style={{ height: 500, position: 'relative', display: 'flex', flexDirection: 'column' }}>
<button onClick={() => mapViewState.fitBounds(bounds, 40)}>Fit bounds</button>
<MapLibreMapView2D state={mapViewState}>
<Marker state={marker} />
</MapLibreMapView2D>
</div>
);
}
Cómo funciona el código
fitBounds ajusta la cámara a un rectángulo geográfico con 40 píxeles de margen. Arrastra el marcador para ajustar el rectángulo entre el inicio y el final del movimiento.