依赖导入

1
npm i @amap/amap-jsapi-loader --save-dev

创建组件

创建一个文件GaodeMap.vue

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<template>
<div>
<div id="container"></div>
</div>
</template>

<script>
import AMapLoader from '@amap/amap-jsapi-loader';

export default {
name: "GaodeMap",
props: {
startPoint: {
type: Array,
default: () => []
},
endPoint: {
type: Array,
default: () => []
}
},
emits: ['pointsSelected'],
data () {
return {
map: null,
startMarker: null,
endMarker: null,
currentPosition: null,
isMarkingStart: true,
polyline: null,
driving: null
};
},
methods: {
initMap () {
this.getCurrentPosition().then((position) => {
this.currentPosition = position;
AMapLoader.reset()
AMapLoader.load({
key: "c556abdb0991ac77cecee08058465e72",
version: "2.0",
plugins: ['AMap.ToolBar', 'AMap.Scale', 'AMap.HawkEye', 'AMap.Driving']
}).then((AMap) => {
this.map = new AMap.Map("container", {
viewMode: "3D",
zooms: [4, 18],
zoom: 16,
center: this.currentPosition
});

this.map.addControl(new AMap.ToolBar());
this.map.addControl(new AMap.Scale());
this.map.addControl(new AMap.HawkEye());

this.driving = new AMap.Driving({
map: this.map
});

if (this.startPoint.length > 0 && this.endPoint.length > 0) {
this.setMarkers(AMap, this.startPoint, this.endPoint);
}

this.map.on('dblclick', (e) => {
this.handleDoubleClick(e, AMap);
});
}).catch((e) => {
console.log(e);
});
}).catch((error) => {
console.error('获取当前位置失败:', error);
this.initMapWithDefaultCenter();
});
},
getCurrentPosition () {
return new Promise((resolve, reject) => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
(position) => {
const { longitude, latitude } = position.coords;
resolve([longitude, latitude]);
},
(error) => {
reject(error);
}
);
} else {
reject(new Error('浏览器不支持地理定位'));
}
});
},
initMapWithDefaultCenter () {
AMapLoader.load({
key: "填写高德的key",
version: "2.0",
plugins: ['AMap.ToolBar', 'AMap.Scale', 'AMap.HawkEye', 'AMap.Driving']
}).then((AMap) => {
this.map = new AMap.Map("container", {
viewMode: "3D",
zooms: [4, 18],
zoom: 16,
center: [105.602725, 37.076636]
});

this.map.addControl(new AMap.ToolBar());
this.map.addControl(new AMap.Scale());
this.map.addControl(new AMap.HawkEye());

this.driving = new AMap.Driving({
map: this.map
});

if (this.startPoint.length > 0 && this.endPoint.length > 0) {
this.setMarkers(AMap, this.startPoint, this.endPoint);
}

this.map.on('dblclick', (e) => {
this.handleDoubleClick(e, AMap);
});
}).catch((e) => {
console.log(e);
});
},
setMarkers (AMap, start, end) {
if (this.startMarker) {
this.startMarker.setMap(null);
}
this.startMarker = new AMap.Marker({
position: start,
title: '起点',
icon: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png'
});
this.startMarker.setMap(this.map);

if (this.endMarker) {
this.endMarker.setMap(null);
}
this.endMarker = new AMap.Marker({
position: end,
title: '终点',
icon: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_r.png'
});
this.endMarker.setMap(this.map);

this.getDrivingRoute(AMap);
},
handleDoubleClick (e, AMap) {
const point = [e.lnglat.getLng(), e.lnglat.getLat()];
if (this.isMarkingStart) {
if (this.startMarker) {
this.startMarker.setMap(null);
}
this.startMarker = new AMap.Marker({
position: point,
title: '起点',
icon: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png'
});
this.startMarker.setMap(this.map);
this.isMarkingStart = false;

if (this.polyline) {
this.polyline.setMap(null);
this.polyline = null;
}
} else {
if (this.endMarker) {
this.endMarker.setMap(null);
}
this.endMarker = new AMap.Marker({
position: point,
title: '终点',
icon: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_r.png'
});
this.endMarker.setMap(this.map);
this.isMarkingStart = true;

this.getDrivingRoute(AMap);
const start = this.startMarker.getPosition();
const end = this.endMarker.getPosition();
const startPoint = [start.getLng(), start.getLat()];
const endPoint = [end.getLng(), end.getLat()];
this.$emit('pointsSelected', { startPoint, endPoint });
}
},
getDrivingRoute (AMap) {
if (this.startMarker && this.endMarker) {
const startPoint = this.startMarker.getPosition();
const endPoint = this.endMarker.getPosition();

console.log(startPoint);
console.log(endPoint);

this.driving.search(startPoint, endPoint, (status, result) => {
if (status === 'complete' && result.info === 'OK') {
const path = result.routes[0].path;
const pathCoords = path.map((p) => [p.lng, p.lat]);

if (this.polyline) {
this.polyline.setMap(null);
}

this.polyline = new AMap.Polyline({
path: pathCoords,
strokeColor: "#FF33FF",
strokeWeight: 2,
strokeOpacity: 1,
strokeStyle: "solid"
});
this.polyline.setMap(this.map);
} else {
console.error('获取驾车路线失败:', result);
}
});
}
}
},
mounted () {
this.initMap();
}
}
</script>

<style scoped>
#container {
width: 100%;
height: 100vh;
border: 1px solid red;
}
</style>

引入组件

在使用的地方引入

1
2
<gaode-map :startPoint="savedStartPoint" :endPoint="savedEndPoint"
@pointsSelected="handlePointsSelected"></gaode-map>

script导入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import GaodeMap from '@/components/GaodeMap.vue'
export default {
data () {
return {
savedStartPoint: [],
savedEndPoint: [],
ruleFoem:{
startPoint:'',
endPoint:''
}
};
},
components: {
GaodeMap,
},
methods:{
handlePointsSelected ({ startPoint, endPoint }) {
this.ruleForm.startPoint = JSON.stringify(startPoint);
this.ruleForm.endPoint = JSON.stringify(endPoint);
},
};
}