Vue AMap Component Library
Vue AMap (vue-amap) is a component library designed for Vue 2.0 applications, providing an abstraction layer for the Gaode (Amap) Map JavaScript API. It enables declarative rendering of various map elements such as markers, polylines, polygons, circles, info windows, and more, leveraging Vue's reactivity system. The current stable version is 0.5.10. This package specifically targets the Vue 2 ecosystem and integrates with the Gaode Maps service, which is primarily used in China. The project appears to have ceased active development and maintenance, making it suitable mainly for legacy Vue 2 applications already using Gaode Maps.
Common errors
-
ReferenceError: Vue is not defined
cause `Vue` object is not globally available or imported when `Vue.use(VueAMap)` is called.fixEnsure `import Vue from 'vue';` is present at the top of your script if you're using a module bundler, or that Vue is loaded via a script tag before your application code in a global context. -
ReferenceError: AMap is not defined
cause The Gaode Map JavaScript API has not been loaded successfully, often because `VueAMap.initAMapApiLoader` was not called, called with an invalid key, or failed to load the external script.fixVerify that `VueAMap.initAMapApiLoader` is called exactly once before any map components are rendered, that `YOUR_GAODE_KEY_HERE` is a valid key, and check network requests for failures to load `https://webapi.amap.com/maps?v=...`. -
[Vue warn]: Unknown custom element: <el-amap> - did you register the component correctly?
cause The `vue-amap` plugin was not registered with Vue, meaning `Vue.use(VueAMap)` was skipped or executed after the component was attempted to be used.fixEnsure `Vue.use(VueAMap);` is called at the very beginning of your application setup, before any Vue instances or components that use `vue-amap` components are created. -
TypeError: Cannot read properties of undefined (reading 'map')
cause This often occurs when map data (e.g., `center`, `markers`, `paths`) is `undefined` or not in the expected format when a `vue-amap` component tries to render it.fixDouble-check that all data bound to `vue-amap` components (e.g., `:center`, `:position`, `:path`) is correctly initialized with arrays or objects as expected by the component, and that it's reactive data.
Warnings
- breaking This package is abandoned. The last commit was in August 2021, and it specifically targets Vue 2, which reached End-of-Life (EOL) in December 2023. It is not compatible with Vue 3 or newer versions.
- gotcha Requires a Gaode Map API key (`key` parameter in `initAMapApiLoader`). This key is essential for the map to load and function correctly. Gaode Maps (Amap) is primarily focused on the Chinese market, and keys might have geographical restrictions.
- gotcha The default Gaode SDK version specified is `1.4.4`. This version might be outdated, potentially lacking new features, performance improvements, or crucial security updates available in later SDK versions. It's unclear if the library is fully compatible with newer Gaode SDK versions.
- gotcha The library registers all its components globally when `Vue.use(VueAMap)` is called. This can lead to naming conflicts if other libraries use similar component names (e.g., `<el-amap>` and `<el-button>` from Element UI, though the prefix 'el-' here is for 'Element' and not 'Element UI').
Install
-
npm install vue-amap -
yarn add vue-amap -
pnpm add vue-amap
Imports
- VueAMap
const VueAMap = require('vue-amap');import VueAMap from 'vue-amap';
- Vue.use(VueAMap)
import { Amap } from 'vue-amap';import VueAMap from 'vue-amap'; import Vue from 'vue'; Vue.use(VueAMap);
- VueAMap.initAMapApiLoader
Vue.initAMapApiLoader({});VueAMap.initAMapApiLoader({ key: 'YOUR_GAODE_KEY', plugin: ['AMap.Autocomplete'], v: '1.4.4' });
Quickstart
import Vue from 'vue';
import VueAMap from 'vue-amap';
Vue.use(VueAMap);
// Initialize vue-amap, load the Gaode Map SDK
VueAMap.initAMapApiLoader({
key: 'YOUR_GAODE_KEY_HERE', // Replace with your actual Gaode Map key
plugin: ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.ToolBar'],
v: '1.4.4' // Specify Gaode SDK version
});
new Vue({
el: '#app',
data() {
return {
zoom: 12,
center: [116.397428, 39.90923],
markers: [{ position: [116.397428, 39.90923] }]
};
},
template: `
<div style="width: 800px; height: 600px;">
<el-amap :zoom="zoom" :center="center">
<el-amap-marker v-for="(marker, index) in markers" :key="index" :position="marker.position"></el-amap-marker>
</el-amap>
</div>
`
});