肖凡4.3提交

yinyuyang^2
肖凡 2024-04-04 16:30:39 +08:00
parent 16b0c1b2a6
commit 184a5a9ddf
1 changed files with 73 additions and 0 deletions

View File

@ -0,0 +1,73 @@
<template>
<div id="container"></div>
</template>
<script>
import AMapLoader from "@amap/amap-jsapi-loader";
export default {
name: "map-view",
data() {
return {
carPosition: [116.397428, 39.90923], //
carMarker: null, // Marker
};
},
mounted() {
this.initAMap();
},
unmounted() {
this.map?.destroy();
},
methods: {
initAMap() {
AMapLoader.load({
key: "e7398786b61ee782417e1749de1f2b39", // WebKey load
version: "2.0", // JSAPI 1.4.15
plugins: ["AMap.Scale"], // 使'AMap.Scale'['...','...']
})
.then((AMap) => {
this.map = new AMap.Map("container", {
// id
viewMode: "3D", // 3D
zoom: 11, //
center: [116.397428, 39.90923], //
});
//
this.carMarker = new AMap.Marker({
position: this.carPosition,
icon: 'https://webapi.amap.com/images/car.png',
map: this.map, // Marker
});
//
this.startCarAnimation();
})
.catch((e) => {
console.log(e);
});
},
startCarAnimation() {
setInterval(() => {
//
this.carPosition = this.getRandomPosition();
// Marker
this.carMarker.setPosition(this.carPosition);
}, 1000);
},
getRandomPosition() {
//
const delta = 0.01; //
const lat = this.carPosition[0] + (Math.random() - 0.5) * delta;
const lng = this.carPosition[1] + (Math.random() - 0.5) * delta;
return [lat, lng];
},
},
};
</script>
<style scoped>
#container {
width: 25%;
height: 250px;
}
</style>