第一步骤
在 index.html 中写上
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=你的密钥&s=1" ></script>
注:如果服务器的访问协议是https,则以上src也需要改为https协议
第二步骤
在 vue.config.js 中写上
第三步骤
公用组件开发
<template>
<div v-show="visible" class="map">
<div id="map-core"></div>
</div>
</template>
<script>
/* eslint-disable */
export default {
name: 'theMap',
data () {
return {
visible:true,
location: {
lng: '',
lat: ''
},
map: {},
ac: {}
}
},
mounted () {
this.setMap()
this.setSearch()
},
methods: {
// 初始化地图
setMap () {
this.map = new BMap.Map('map-core')
this.map.centerAndZoom(new BMap.Point(113.275, 23.117), 10)
// 地图缩放控件
const topLeftControl = new BMap.ScaleControl({anchor: BMAP_ANCHOR_BOTTOM_LEFT})
// 城市选择控件
const cityListControl = new BMap.CityListControl({anchor: BMAP_ANCHOR_TOP_RIGHT})
// 比例尺控件
const topLeftNavigation = new B