關於Vue.js引入百度地圖的插件


只為了記錄自己所學習的知識,歡迎大佬指點

1、在項目中安裝地圖插件

npm install vue-baidu-map --save

2、在根目錄下的index.html頁面中引入地圖路徑

密匙可以在https://lbsyun.baidu.com/index.php?title=jspopularGL/guide/getkey中獲取
<script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=你的密匙"></script>

前期的工作算是做完了接下來就可以在你想要的模塊或者組件中去使用舉個例子:

<template>
        <div id="page">
        
        </div>
</template>

<script>
export default {
    data(){
        return {};
    },
    mounted(){
        this.mapdata();
    },
    methods: {
        mapdata(){
            //創建地圖實例
            let map = new BMap.Map("page");
            //創建點坐標
            let point = new BMap.Point(116.404, 39.915);
            //初始化地圖,設置中心點坐標和地圖級別
            map.centerAndZoom(point,15);
            //創建標注
            let initMarker = new BMap.Marker(point);
            //向地圖中添加單個覆蓋物時會觸發此事件
            map.addOverlay(initMarker);
            //開啟標注拖拽功能
            initMarker.enableDragging();
            //將標注點移動到中心位置
            /*
            panBy:將地圖在水平位置上移動x像素,垂直位置上移動y像素。
            如果指定像素大於可視區域范圍或者在配置這種指定沒有動畫
            效果,則不執行滑動效果 
             */
            map.panBy(350,225);
            //添加地圖默認控件
            map.addControl(new BMap.NavigationControl());
            //開啟鼠標滾輪縮放
            map.enableScrollWheelZoom(true);
        }
    },
};
</script>

<style scope>
    #page{
        width: 700px;
        height: 450px;
    }
</style>

接下來運行即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM