vue-mapbox-gl

repository·develop·Indexed 41 days ago

https://github.com/studiometa/vue-mapbox-gl

A lightweight Vue 3 component library for integrating Mapbox GL into applications. Version 2.7.2 provides high-level components such as MapboxMap, MapboxMarker, MapboxCluster, MapboxGeocoder, MapboxLayer, and MapboxPopup to manage maps and geospatial data within the Vue ecosystem.

Tokens
12.2K
Snippets
28
Records
64
Agent score
51%

What's inside vue-mapbox-gl

  1. Overview of @studiometa/vue-mapbox-gl

    develop
    @studiometa/vue-mapbox-gl is a small components library designed to integrate Mapbox GL into Vue 3 applications. It provides a set of Vue components to easily manage maps, markers, clusters, geocoders, and more within the Vue ecosystem.
  2. Use the StoreLocator component

    develop

    The StoreLocator component displays a clustered list of points of interest (POIs) on a map and in an adjacent filtered list. To use it, you must import the component, its CSS, and the required Mapbox GL and Geocoder CSS files.

    <script setup>
      // Import the component and its CSS
      import { StoreLocator } from '@studiometa/vue-mapbox-gl';
      import '@studiometa/vue-mapbox-gl/index.css';
    
      // Import Mapbox required CSS stylesheets
      import 'mapbox-gl/dist/mapbox-gl.css';
      import '@mapbox/mapbox-gl-geocoder/lib/mapbox-gl-geocoder.css';
    </script>
    
    <template>
      <StoreLocator>
        <!-- Content goes here -->
      </StoreLocator>
    </template>
  3. Use MapboxGeolocateControl

    develop

    The MapboxGeolocateControl component adds a button to the map that allows users to center the map on their current GPS location. Note that the button will not be displayed if the user has blocked access to their geolocation.

    <script setup>
      import { MapboxMap, MapboxGeolocateControl } from '@studiometa/vue-mapbox-gl';
    </script>
    
    <MapboxMap
      style="height: 400px"
      access-token="..."
      map-style="mapbox://styles/mapbox/streets-v11">
      <MapboxGeolocateControl />
    </MapboxMap>
  4. Use the MapboxImages component

    develop

    The MapboxImages component allows you to add multiple images to a Mapbox GL map simultaneously. These images can then be referenced in Mapbox GL layers using properties like icon-image, background-pattern, fill-pattern, and line-pattern. It acts as a bulk wrapper around the MapboxImage component.

    To use it, provide a sources array containing image definitions. The component emits a ready event once all images are successfully loaded and added to the map.

    <MapboxImages
      :sources="[
        { id: 'my-icon', src: 'https://example.com/icon.png' },
        { id: 'pattern', src: '/local-pattern.png', options: { sdf: true } }
      ]"
    />
  5. Use the MapboxImage component

    develop

    The MapboxImage component allows you to add an image to the Mapbox GL map instance. Once added, the image can be referenced in Mapbox GL layers using properties like icon-image, background-pattern, fill-pattern, and line-pattern.

    To use it, place the MapboxImage component inside a MapboxMap component. You can also use the default slot to render child components (like MapboxLayer) that depend on the image being loaded. The default slot is rendered only after the image has been successfully added to the map and the add event is emitted.

    <template>
      <MapboxMap
        access-token="YOUR_TOKEN"
        map-style="mapbox://styles/mapbox/streets-v11"
        :center="[0, 0]"
        :zoom="1">
        
        <MapboxImage
          src="https://example.com/icon.png"
          id="my-icon">
          <!-- The default slot ensures the layer only renders once the image is ready -->
          <MapboxLayer
            id="points-layer"
            :options="{
              type: 'symbol',
              source: { type: 'geojson', data: ... },
              layout: { 'icon-image': 'my-icon' }
            }" />
        </MapboxImage>
    
      </MapboxMap>
    </template>
  6. Use the MapboxGeocoder component

    develop

    The MapboxGeocoder component displays a search form to search for places using the Mapbox Geocoding API. It can be used inside a <MapboxMap> component or as a standalone component outside of a map.

    Important Requirements:

    • You must manually import the Mapbox Geocoder stylesheet: import '@mapbox/mapbox-gl-geocoder/lib/mapbox-gl-geocoder.css';.
    • If using the component outside of a <MapboxMap>, you must provide the accessToken prop.
    • This component requires @mapbox/mapbox-gl-geocoder version 4.5.0 or higher.
    <template>
      <MapboxMap
        style="margin-top: 1em; height: 400px;"
        :access-token="MAPBOX_API_KEY"
        map-style="mapbox://styles/mapbox/streets-v11">
        <MapboxGeocoder />
      </MapboxMap>
    </template>
    
    <script>
      import '@mapbox/mapbox-gl-geocoder/lib/mapbox-gl-geocoder.css';
    </script>
  7. Install required peer dependencies

    develop

    To ensure the components function correctly, you must manually install the following peer dependencies:

    1. mapbox-gl: Required for all map components.
    2. @mapbox/mapbox-gl-geocoder: Only required if you intend to use the MapboxGeocoder component.
    # Install mapbox-gl
    npm install mapbox-gl
    
    # The geocoder package is only required if you use the `MapboxGeocoder` component
    npm install @mapbox/mapbox-gl-geocoder
  8. Basic usage of @studiometa/vue-mapbox-gl

    develop

    To use the library, import the necessary components like MapboxMap and MapboxMarker from @studiometa/vue-mapbox-gl. You must also import the Mapbox GL stylesheet (mapbox-gl/dist/mapbox-gl.css) for the map to render correctly. Provide your Mapbox access-token to the MapboxMap component.

    <script setup>
      import { MapboxMap, MapboxMarker } from '@studiometa/vue-mapbox-gl';
      import 'mapbox-gl/dist/mapbox-gl.css';
    </script>
    
    <template>
      <MapboxMap access-token="...">
        <MapboxMarker position="[0, 0]" />
      </MapboxMap>
    </template>