vue-baidu-map-3x vue3中让marker移动到坐标位置
在Vue3中,可以使用vue-baidu-map-3x组件库来实现marker移动到指定坐标位置。
首先,安装vue-baidu-map-3x库:
npm install vue-baidu-map-3x
然后,在需要使用地图的组件中,导入和注册vue-baidu-map-3x:
import VueBaiduMap from 'vue-baidu-map-3x';
export default {
components: {
VueBaiduMap,
},
// ...
}
接下来,在模板中使用vue-baidu-map-3x组件,并定义地图的中心点和marker的坐标:
<template>
<vue-baidu-map :ak="yourBaiduMapAK" :center="mapCenter" :zoom="13" style="width: 100%; height: 400px;">
<vue-baidu-map-marker :position="markerPosition" :animation="animation"></vue-baidu-map-marker>
</vue-baidu-map>
</template>
在上面的代码中,yourBaiduMapAK是你申请的百度地图AK,mapCenter是地图的中心点坐标,markerPosition是marker的坐标,animation是marker的动画效果。
最后,在Vue组件的data中定义地图的中心点和marker的坐标,并提供一个方法来移动marker到指定的坐标位置:
export default {
data() {
return {
yourBaiduMapAK: 'yourBaiduMapAK',
mapCenter: { lng: 116.404, lat: 39.915 },
markerPosition: { lng: 116.404, lat: 39.915 },
animation: null,
};
},
methods: {
moveMarkerToPosition(position) {
this.markerPosition = position;
},
},
// ...
}
现在,当调用moveMarkerToPosition方法并提供新的坐标位置时,marker将会移动到指定的位置。
希望对你有帮助
原文地址: https://www.cveoy.top/t/topic/inrz 著作权归作者所有。请勿转载和采集!