MapConductor React SDK Samples

सीमा में फ़िट करें | MapLibre | MapConductor React SDK

MapLibre पर MapConductor React SDK का सीमा में फ़िट करें सैंपल।

यह इंटरैक्टिव उदाहरण MapLibre पर MapConductor की एब्स्ट्रैक्शन के ज़रिए सीमा में फ़िट करें दिखाता है।

कोड उदाहरण

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) मैप बनाएँ


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


  const mapViewState = useMapLibreViewState({
    mapDesignType: MapLibreDesign.OsmBrightJa,
    cameraPosition: initialCamera,
  });


  // (2) डेटा और इंटरैक्शन स्टेट तैयार करें
  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) मैप और उसके ओवरले रेंडर करें


  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>
  );
}

कोड कैसे काम करता है

fitBounds कैमरे को 40 पिक्सेल के अंतर के साथ भौगोलिक आयत में फ़िट करता है। मार्कर खींचने पर आरंभ और अंत के बीच का क्षेत्र दिखता है।