shadcn-map

repository·main·Indexed 43 days ago

https://github.com/tonghohin/shadcn-map

A specialized map component library for shadcn/ui built with Leaflet and React Leaflet. Version 0.1.0 provides a suite of components including Map, MapTileLayer with automatic theme switching via next-themes, MapMarker with clustering, and various drawing and control tools like MapZoomControl, MapFullscreenControl, and MapSearchControl.

Tokens
8.8K
Snippets
39
Records
57
Agent score
56%

What's inside shadcn-map

  1. Overview of shadcn/ui Map

    main
    shadcn/ui Map is a map component library built using Leaflet and React Leaflet. It is specifically designed to integrate seamlessly with the shadcn/ui ecosystem, providing a map interface that follows shadcn's design language and component patterns.
  2. Manage multiple layers with MapLayers and MapLayersControl

    main

    To allow users to switch between different tile layers or toggle layer groups, you must use the MapLayers context and the MapLayersControl UI component.

    MapLayers (Context Provider)

    Required when using multiple tile layers or layer groups together, especially if you want to use MapLayersControl.

    • defaultTileLayer: The name of the MapTileLayer to select by default.
    • defaultLayerGroups: An array of names of the MapLayerGroups to activate by default.

    MapLayersControl (UI Component)

    Renders a UI for selecting the active tile layer and toggling layer groups.

    • Note: This component is only visible if there are multiple tile layers or layer groups registered.
    • tileLayersLabel: Custom label for the tile layers section (Defaults to Map Type).
    • layerGroupsLabel: Custom label for the layer groups section (Defaults to Layers).
    • position: Custom position (Defaults to top-1 right-1).
    • Must be used inside a MapLayers component.
    <Map center={[51.5, -0.09]}>
      <MapLayers defaultTileLayer="Satellite">
        <MapTileLayer name="Street" url="..." />
        <MapTileLayer name="Satellite" url="..." />
        <MapLayersControl />
      </MapLayers>
    </Map>
  3. Group layers with MapLayerGroup and MapFeatureGroup

    main

    Use these components to manage the visibility of multiple layers.

    MapLayerGroup

    Used for grouping multiple map layers to control their visibility as a single unit.

    • name (Required): A unique name for the group.
    • disabled: If true, prevents users from toggling this group in the layer control.

    MapFeatureGroup

    Similar to MapLayerGroup, but provides extra capabilities:

    • You can bind popups or tooltips to all child layers at once.
    • Events on the group automatically propagate to its child layers.
    • name (Required): A unique name for the group.
    • disabled: If true, prevents users from toggling this group in the layer control.
  4. Toggle visibility of Layer Groups with MapLayersControl

    main

    To group related markers or features and allow users to toggle their visibility, wrap MapLayerGroup components inside a MapLayers container and include the MapLayersControl component.

    Use the defaultLayerGroups prop on the MapLayers container to specify which groups should be visible when the map first loads.

    <MapLayers defaultLayerGroups={['group-id-1']}>
      <MapLayerGroup id="group-id-1">
        {/* Markers or features here */}
      </MapLayerGroup>
      <MapLayersControl />
    </MapLayers>
  5. Handle location success and error callbacks in Locate Control

    main

    You can pass optional callback functions to the LocateControl to react when the user's location is successfully retrieved or when an error occurs (e.g., if the user denies permission).

    // Example pattern for using callbacks with Locate Control
    <LocateControl 
      onLocationSuccess={(location) => {
        console.log('User location:', location);
      }}
      onLocationError={(error) => {
        console.error('Failed to get location:', error);
      }}
    />
  6. Handle selected places with onPlaceSelect

    main

    To react when a user selects a specific place from the autocomplete results, pass an onPlaceSelect callback to the search control.

    Important: Because useMap can only be used inside a Map container, you must wrap the search control in a client-side wrapper component if you intend to perform map actions (like panning, zooming, or adding markers) based on the selection.

    // Example of a client-side wrapper component
    const MapSearchWrapper = () => {
      const map = useMap();
    
      const handlePlaceSelect = (place: any) => {
        // Perform map actions like panning to the selected location
        map.panTo(place.location);
        // Or add a marker
      };
    
      return (
        <SearchControl onPlaceSelect={handlePlaceSelect} />
      );
    };
  7. Access search results with onResultsChange

    main

    You can use the onResultsChange callback to receive the list of search results as the user types. This is useful for displaying markers on the map for every result returned by the search engine.

    <SearchControl 
      onResultsChange={(results) => {
        // Handle results, e.g., displaying markers for each result
        console.log(results);
      }} 
    />