新建 components/amap.vue
文件,封装高德地图组件:
<template>
<div>
<div id="amapcontainer" style="width: 800px; height: 520px"></div>
</div>
</template>
<script>
import AMapLoader from '@amap/amap-jsapi-loader';
window._AMapSecurityConfig = {
securityJsCode: '' // '「申请的安全密钥」',
}
export default {
name: "purchannel",
data () {
return {
zoom: 12,
originX: '',// 当前浏览器定位的位置
originY: '',
map: null, //地图实例
infoWindow: '',
makerList: [],
provinceArr: [],
cityArr: [],
provinceCode: '',
cityCode: '',
storeList: [], //门店列表
}
},
watch: {
},
created () {
},
mounted () {
// DOM初始化完成进行地图初始化
this.initAMap()
},
methods: {
initAMap () {
AMapLoader.load({
key: "", // 申请好的Web端开发者Key,首次调用 load 时必填
version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
plugins: ["AMap.Scale", "AMap.ToolBar", "AMap.ControlBar", 'AMap.Geocoder', 'AMap.Marker',
'AMap.CitySearch', 'AMap.Geolocation', 'AMap.AutoComplete', 'AMap.InfoWindow'], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
}).then((AMap) => {
// 获取到作为地图容器的DOM元素,创建地图实例
this.map = new AMap.Map("amapcontainer", {
//设置地图容器id
resizeEnable: true,
zoom: this.zoom, // 地图显示的缩放级别
viewMode: "3D", // 使用3D视图
zoomEnable: true, // 地图是否可缩放,默认值为true
dragEnable: true, // 地图是否可通过鼠标拖拽平移,默认为true
doubleClickZoom: true, // 地图是否可通过双击鼠标放大地图,默认为true
// center: [113.370824, 23.131265], // 中心点坐标 广州
// mapStyle: "amap://styles/darkblue", // 设置颜色底层
});
this.getLocation()
this.setupMapTool()