Vue 3 Plugin for MapLibre GL JS
Vue-MapLibre-GL is a small, actively maintained Vue 3 plugin that provides components and utilities for integrating MapLibre GL JS into Vue applications. The current stable version is 5.6.1, with minor releases occurring frequently, indicating active development and responsiveness to bug fixes and feature enhancements. Key differentiators include full TypeScript support, a comprehensive set of Vue components for managing maps, controls, sources, markers, and layers, and a built-in 'Simple Draw Control' that allows users to draw polygons, circles, and static circles. It also features support for multiple map instances via the `useMap` composition API hook, customizable style switching, language toggling, and automatic map restart on `CONTEXT_LOST_WEBGL` events, which is particularly useful for mobile device stability.
Common errors
-
Cannot find module 'maplibre-gl' or its corresponding type declarations.
cause The `maplibre-gl` package, a peer dependency, has not been installed.fixInstall the `maplibre-gl` package: `npm install maplibre-gl` or `yarn add maplibre-gl`. -
Failed to resolve component: MglMap
cause The `MglMap` component (or other `Mgl` components) has not been correctly registered globally via `app.use(VueMaplibreGl)` or locally imported and added to the `components` option of a Vue component.fixEnsure `import { MglMap } from 'vue-maplibre-gl'` is present and `MglMap` is listed in the `components` object of your Vue component, or perform a global installation using `app.use(VueMaplibreGl)` in your main.js/ts file. -
TypeError: Cannot read properties of undefined (reading 'addControl')
cause Attempting to use a MapLibre GL JS control (e.g., `NavigationControl`) without initializing it or adding it to an `MglMap` instance correctly. This might also happen if the map instance is not yet ready.fixEnsure controls are instantiated and added within the context of an `MglMap` component or its `map` instance, typically after the map `load` event. Use `Mgl*Control` components provided by `vue-maplibre-gl` for easier integration.
Warnings
- gotcha When using the `MglDrawControl` component, additional dependencies from Turf.js are required at runtime. These are not always listed as explicit peer dependencies and must be installed separately to avoid runtime errors related to missing geospatial functions.
- gotcha The Draw Plugin CSS (`vue-maplibre-gl-draw.css`) must be explicitly imported if you intend to use the `MglDrawControl` component. The base `vue-maplibre-gl.css` only provides styles for the core map and controls.
- breaking While not explicitly documented in the `vue-maplibre-gl` changelog, a major version bump in the underlying `maplibre-gl` library (e.g., from v5 to v6) could introduce breaking changes that might affect `vue-maplibre-gl`'s functionality or require updates to map styles or data handling.
- gotcha The library automatically restarts the map on `CONTEXT_LOST_WEBGL` events, which commonly occur on mobile devices when a tab is in the background for an extended period. While a feature to improve reliability, developers should be aware that this can lead to temporary map reinitialization.
Install
-
npm install vue-maplibre-gl -
yarn add vue-maplibre-gl -
pnpm add vue-maplibre-gl
Imports
- VueMaplibreGl
const VueMaplibreGl = require('vue-maplibre-gl')import VueMaplibreGl from 'vue-maplibre-gl'
- MglMap
import MglMap from 'vue-maplibre-gl'
import { MglMap } from 'vue-maplibre-gl' - useMap
VueMaplibreGl.useMap()
import { useMap } from 'vue-maplibre-gl'
Quickstart
import { createApp } from 'vue';
import { MglMap, MglNavigationControl } from 'vue-maplibre-gl';
import 'maplibre-gl/dist/maplibre-gl.css';
import 'vue-maplibre-gl/dist/vue-maplibre-gl.css';
// import 'vue-maplibre-gl/dist/vue-maplibre-gl-draw.css'; // Uncomment if using MglDrawControl
const app = createApp({
components: {
MglMap,
MglNavigationControl,
},
data() {
return {
mapStyle: 'https://demotiles.maplibre.org/style.json', // Or any other MapLibre GL compatible style URL
center: [-74.0060, 40.7128], // New York City coordinates
zoom: 10,
};
},
template: `
<div style="width: 100%; height: 500px;">
<mgl-map
:map-style="mapStyle"
:center="center"
:zoom="zoom"
>
<mgl-navigation-control position="top-right" />
</mgl-map>
</div>
`,
});
app.mount('#app');