Vue Baidu Map Components

0.21.22 · maintenance · verified Sun Apr 19

Vue Baidu Map is a component library providing Baidu Map integration for Vue.js 2.x applications. It allows developers to easily embed interactive Baidu Maps and leverage various features like markers, overlays, and controls within their Vue projects. The current stable version is 0.21.22. While it has seen recent updates (as of September 2023), its explicit focus on Vue 2.x places it in a maintenance state, especially given Vue 2's End of Life in December 2023. Key differentiators include its tight integration with Baidu's localized mapping services, which are critical for applications targeting users in mainland China, where other global map providers may have limited or no service.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the installation, initialization with an API key, and basic usage of the `<baidu-map>` component with a marker and zoom controls in a Vue 2 application. It highlights the essential setup and provides styling.

import Vue from 'vue';
import BaiduMap from 'vue-baidu-map';

Vue.use(BaiduMap, {
  // Obtain your Baidu Map API Key from http://lbsyun.baidu.com/apiconsole/key
  ak: process.env.VUE_APP_BAIDUMAP_AK ?? 'YOUR_APP_KEY' // Replace with your actual key
});

new Vue({
  el: '#app',
  template: `
    <div id="app">
      <h1>My Baidu Map</h1>
      <baidu-map class="map-container" :center="{lng: 116.404, lat: 39.915}" :zoom="15">
        <bm-marker :position="{lng: 116.404, lat: 39.915}" :dragging="true" animation="BMAP_ANIMATION_BOUNCE"></bm-marker>
        <bm-control anchor="BMAP_ANCHOR_TOP_LEFT">
          <button @click="zoom(3)">Increase Zoom</button>
          <button @click="zoom(-3)">Decrease Zoom</button>
        </bm-control>
      </baidu-map>
    </div>
  `,
  methods: {
    zoom(levelChange) {
      // This method would typically interact with the map instance
      // For a full example, you'd need a ref to the bm-map component
      console.log('Zoom changed by', levelChange);
    }
  },
  components: { BaiduMap } // Not strictly needed for global plugin but good practice for clarity
});

// Basic CSS for the map container
const style = document.createElement('style');
style.innerHTML = `
  .map-container {
    width: 100%;
    height: 400px;
    border: 1px solid #ccc;
  }
`;
document.head.appendChild(style);

view raw JSON →