去除掉dialog弹窗, 直接展示地图, 下一步是做轨迹回放
相关文档
bash
https://developer.amap.com/demo/javascript-api-v2/example/marker/replaying-historical-running-data
https://blog.csdn.net/qq_41339126/article/details/116596465
html
<template>
<div class="map-container">
<div id="map" style="height: 800px; width: 80%;margin-left: 200px;" />
</div>
</template>
<script>
import { ref, onMounted } from 'vue'
import AMapLoader from '@amap/amap-jsapi-loader'
export default {
setup() {
const map = ref(null)
const initMap = async () => {
AMapLoader.load({
key: '-', // 你的高德地图key
version: '2.0',
plugins: ['AMap.Marker'],
})
.then((AMap) => {
map.value = new AMap.Map('map', {
viewMode: '3D', // 是否为3D地图模式
zoom: 15, // 初始化地图级别
resizeEnable: true, // 是否监控地图容器尺寸变化
mapStyle: 'amap://styles/normal',
center: [116.361124, 39.959828], // 初始化地图中心点位置
showMarker: true, // 定位成功后在定位到的位置显示点标记,默认:true
})
// 添加标记
const marker = new AMap.Marker({
position: [116.361124, 39.959828],
icon: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_bs.png',
draggable: true,
// map: map.value,
})
map.value.add(marker)
})
.catch((e) => {
console.log(e)
})
}
onMounted(() => {
window._AMapSecurityConfig = {
securityJsCode: '-',
}
initMap() // 初始化地图
})
return {
map,
initMap,
}
},
}
</script>
<style>
.map-container {
margin: 10px 0;
}
#map {
height: 400px;
width: 100%;
}
</style>
- 添加标记接口
xml
<template>
<div class="map-container">
<div id="map" style="height: 300px; width: 50%; margin-left: 200px;" />
<el-button style="margin-left: 200px;" @click="addMarker">添加标记</el-button>
</div>
</template>
<script>
import { ref, onMounted } from 'vue'
import AMapLoader from '@amap/amap-jsapi-loader'
export default {
setup() {
const map = ref(null)
let marker = null // 将 marker 变量移至 setup 函数外部
const initMap = async () => {
AMapLoader.load({
key: '',
version: '2.0',
plugins: ['AMap.Marker'],
})
.then((AMap) => {
map.value = new AMap.Map('map', {
viewMode: '3D',
zoom: 15,
resizeEnable: true,
mapStyle: 'amap://styles/normal',
center: [116.361124, 39.959828],
showMarker: true,
})
// 初始化标记
marker = new AMap.Marker({
position: [116.361124, 39.959828],
icon: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_bs.png',
draggable: true,
})
map.value.add(marker)
})
.catch((e) => {
console.log(e)
})
}
const addMarker = () => {
if (map.value) {
if (marker) {
console.log("移除旧标记");
map.value.remove(marker)
}
marker = new AMap.Marker({
position: [116.361124, 39.959829],
icon: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_bs.png',
draggable: true,
})
map.value.add(marker)
}
}
onMounted(() => {
window._AMapSecurityConfig = {
securityJsCode: '',
}
initMap()
})
return {
addMarker,
}
},
}
</script>
<style>
.map-container {
margin: 10px 0;
}
#map {
height: 400px;
width: 100%;
}
</style>
- 初步实现轨迹回复
xml
<template>
<div class="map-container">
<div id="map" style="height: 300px; width: 50%; margin-left: 200px;"></div>
<el-button style="margin-left: 200px;" @click="addMarker">添加标记</el-button>
<div class="input-card">
<h4>轨迹回放控制</h4>
<div class="input-item">
<input type="button" class="btn" value="开始动画" @click="startAnimation" />
<input type="button" class="btn" value="暂停动画" @click="pauseAnimation" />
</div>
<div class="input-item">
<input type="button" class="btn" value="继续动画" @click="resumeAnimation" />
<input type="button" class="btn" value="停止动画" @click="stopAnimation" />
</div>
</div>
</div>
</template>
<script>
import { ref, onMounted } from 'vue'
import AMapLoader from '@amap/amap-jsapi-loader'
export default {
setup() {
const map = ref(null)
let marker = null
let polyline = null
let passedPolyline = null
const lineArr = [[116.478935,39.997761],[116.478939,39.997825],[116.478912,39.998549],[116.478912,39.998549],[116.478998,39.998555],[116.478998,39.998555],[116.479282,39.99856],[116.479658,39.998528],[116.480151,39.998453],[116.480784,39.998302],[116.480784,39.998302],[116.481149,39.998184],[116.481573,39.997997],[116.481863,39.997846],[116.482072,39.997718],[116.482362,39.997718],[116.483633,39.998935],[116.48367,39.998968],[116.484648,39.999861]];
const initMap = async () => {
AMapLoader.load({
key: '-',
version: '2.0',
plugin: ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.OverView',
'AMap.ToolBar', 'AMap.MapType', 'AMap.PolyEditor', 'AMap.CircleEditor', 'AMap.MarkerClusterer', 'AMap.MoveAnimation'],
}).then((AMap) => {
AMap.plugin('AMap.MoveAnimation', function(){});
map.value = new AMap.Map('map', {
viewMode: '3D',
zoom: 15,
resizeEnable: true,
mapStyle: 'amap://styles/normal',
center: [116.478935,39.997761],
showMarker: true
})
// Initialize marker
marker = new AMap.Marker({
position: [116.478935,39.997761],
icon: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_bs.png',
draggable: true
})
map.value.add(marker)
// Draw polyline
polyline = new AMap.Polyline({
map: map.value,
path: lineArr,
showDir: true,
strokeColor: '#28F',
strokeWeight: 6
})
passedPolyline = new AMap.Polyline({
map: map.value,
strokeColor: '#AF5',
strokeWeight: 6
})
marker.on('moving', function (e) {
passedPolyline.setPath(e.passedPath)
})
map.value.setFitView()
})
.catch((e) => {
console.log(e)
})
}
const addMarker = () => {
if (map.value) {
if (marker) {
console.log('移除旧标记')
map.value.remove(marker)
}
marker = new AMap.Marker({
position: [116.361124, 39.959829],
icon: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_bs.png',
draggable: true
})
map.value.add(marker)
}
}
const startAnimation = () => {
if (marker) {
marker.moveAlong(lineArr, 200)
}
}
const pauseAnimation = () => {
if (marker) {
marker.pauseMove()
}
}
const resumeAnimation = () => {
if (marker) {
marker.resumeMove()
}
}
const stopAnimation = () => {
if (marker) {
marker.stopMove()
}
}
onMounted(() => {
window._AMapSecurityConfig = {
securityJsCode: '-'
}
initMap()
})
return {
addMarker,
startAnimation,
pauseAnimation,
resumeAnimation,
stopAnimation
}
}
}
</script>
<style>
.map-container {
margin: 10px 0;
}
#map {
height: 400px;
width: 100%;
}
</style>