MapConductor React SDK Samples

Globo simple | MapLibre | MapConductor React SDK

Ejemplo de Globo simple de MapConductor React SDK con MapLibre.

Este ejemplo interactivo muestra Globo simple mediante la abstracción de MapConductor sobre MapLibre.

Ejemplo de código

import { useMemo, useState } from 'react';
import { createGeoPoint, createMapCameraPosition, createMarkerState } from '@mapconductor/js-sdk-core';
import { InfoBubble, 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 position = useMemo(() => createGeoPoint({ latitude: 35.6812, longitude: 139.7671 }), []);
  const [selected, setSelected] = useState(false);
  const marker = useMemo(() => createMarkerState({
    id: 'place', position, onClick: () => setSelected(true),
  }), [position]);


  // (3) Renderizar el mapa y sus capas


  return (
    <div style={{ height: 500, position: 'relative', display: 'flex', flexDirection: 'column' }}>
      <MapLibreMapView2D state={mapViewState}>
        <Marker state={marker} />
        {selected && (
          <InfoBubble marker={marker}>Simple text content</InfoBubble>
        )}
      </MapLibreMapView2D>
    </div>
  );
}

Cómo funciona el código

Ancla un globo sencillo con contenido React al marcador seleccionado por el usuario.

Una bandera booleana selected vive en useState, y el onClick del marcador la cambia a true.

El InfoBubble se dibuja solo mientras selected es true y contiene texto plano, mostrando la forma mínima de un globo anclado a un marcador.