實(shí)現(xiàn)方法:1、在“百度地圖開發(fā)平臺(tái)”中申請(qǐng)秘鑰;2、在“index.html”中使用script標(biāo)簽引入地圖鏈接;3、在“APP.vue”中放入相關(guān)js代碼實(shí)現(xiàn)百度地圖。
本教程操作環(huán)境:windows7系統(tǒng)、vue2.9.6版,DELL G3電腦。
vue在項(xiàng)目中使用百度地圖
第一步,去百度地圖開發(fā)平臺(tái)http://lbsyun.baidu.com/ 申請(qǐng)秘鑰。
第二步在項(xiàng)目中引入,具體如下
其中index.html存放地圖鏈接,代碼如下
然后在APP.vue里面實(shí)現(xiàn),代碼如下
<template> <div id="app"> <div id="allmap" ref="allmap"></div> <router-view></router-view> </div> </template> <script> export default { name: 'App', methods:{ map(){ let map =new BMap.Map(this.$refs.allmap); // 創(chuàng)建Map實(shí)例 map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);// 初始化地圖,設(shè)置中心點(diǎn)坐標(biāo)和地圖級(jí)別 map.addControl(new BMap.MapTypeControl({//添加地圖類型控件 mapTypes:[ BMAP_NORMAL_MAP, BMAP_HYBRID_MAP ]})); map.setCurrentCity("北京");// 設(shè)置地圖顯示的城市 此項(xiàng)是必須設(shè)置的 map.enableScrollWheelZoom(true); //開啟鼠標(biāo)滾輪縮放 } }, mounted(){ this.map() } } </script> <style> #app { font-family: 'Avenir', Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px; } #allmap{ height: 500px; overflow: hidden; } </style>