vue引入高德地图

1. 官方获取key:https://console.amap.com/dev/key/app

2. index.html引入

1
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=你申请的key"></script> 

3.配置build/webpack.base.conf.js

1
2
3
externals: {
'AMap': 'AMap'
},

重启项目,使用

我封装成组件了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<template>
<el-dialog
width="65%"
:append-to-body="true"
:modal-append-to-body="false"
title="地图"
:visible="show"
:before-close="close"
>
<div>
<div class="search">
<el-input id="tipinput" v-model="searchText"></el-input>
</div>
<div id="amap"></div>
</div>
<span slot="footer" class="dialog-footer">
<el-button @click="close">取 消</el-button>
<el-button type="primary" @click="ok">确 定</el-button>
</span>
</el-dialog>
</template>

<script>
/**
* @author 全易
* @time 2020-12-20 10:18:12 星期天
* @description 高德地图
*/

export default {
name: "Amap",
props: {
show: {
type: Boolean,
default: false,
},
},
data() {
return {
map: null,
searchText: "",
data: {},
};
},
mounted() {
console.log("高德地图:", AMap);
},
watch: {
show(now, old) {
if (now) {
this.$nextTick(() => {
this.amap();
});
}
},
},
methods: {
// 初始化地图
amap() {
this.map = new AMap.Map("amap", {
resizeEnable: true,
zoom: 11,
center: [116.397428, 39.90923],
});

this.map.plugin(["AMap.ToolBar"], function () {
this.map.addControl(new AMap.ToolBar());
});
if (location.href.indexOf("&guide=1") !== -1) {
this.map.setStatus({ scrollWheel: false });
}

this.click();

//输入提示
var auto = new AMap.Autocomplete({
input: "tipinput",
});
},
// 点击地图
click() {
const that = this;
this.map.on("click", function (e) {
console.log(e);
that.data = e;
// console.log(e.lnglat.getLng() + "," + e.lnglat.getLat());
});
},
// 关闭地图面板
close() {
this.$emit("close", false);
},
// 确定
ok() {
this.$emit("ok", this.data);
this.close();
},
},
};
</script>

<style lang="less" scoped>
#amap {
width: 100%;
height: 60vh;
}
</style>

vue引入高德地图
https://github.com/chergn/chergn.github.io/79b09725d61e/
作者
全易
发布于
2024年3月28日
许可协议