Access the MapLibre GL instance for custom features.
Add GeoJSON data with fill and outline styles. Hover for interactions.
For 100s-1000s of markers, use GeoJSON layers instead of DOM markers for WebGL performance.
Call map methods from event handlers or effects.
import { Map, type MapRef } from "@/components/ui/map";
import { useRef } from "react";
function MyMapComponent() {
const mapRef = useRef<MapRef>(null);
const handleFlyTo = () => {
mapRef.current?.flyTo({ center: [-74, 40.7], zoom: 12 });
};
return (
<>
<button onClick={handleFlyTo}>Fly to NYC</button>
<Map ref={mapRef} center={[-74, 40.7]} zoom={10} />
</>
);
}For child components inside Map, use useMap.
import { Map, useMap } from "@/components/ui/map";
import { useEffect } from "react";
function MapEventListener() {
const { map, isLoaded } = useMap();
useEffect(() => {
if (!map || !isLoaded) return;
const handleClick = (e) => {
console.log("Clicked at:", e.lngLat);
};
map.on("click", handleClick);
return () => map.off("click", handleClick);
}, [map, isLoaded]);
return null;
}
<Map center={[-74, 40.7]} zoom={10}>
<MapEventListener />
</Map>