vue集成高德地圖amap-jsapi-loader的實現(xiàn)
前往高德地圖開發(fā)平臺高德開放平臺 | 高德地圖API
一:申請高德key
去高德官網(wǎng)去創(chuàng)建一個屬于自己的地圖應(yīng)用 (得到key和秘鑰) 。
首先,我們要注冊一個開發(fā)者賬號,根據(jù)實際情況填寫,身份寫個人:

進(jìn)入我的應(yīng)用: ?????

創(chuàng)建新應(yīng)用


獲取key和密鑰

注意: 2021年12月02日后創(chuàng)建的 key 必須配備安全密鑰一起使用
二:在vue中使用高德地圖
這里使用的是amap-jsapi-loader 。@amap/amap-jsapi-loader是一個用于加載高德地圖JavaScript API的工具庫。它可以幫助開發(fā)者快速、簡便地在網(wǎng)頁中引入高德地圖API,并提供了一些方便的配置選項和回調(diào)函數(shù),以便開發(fā)者更好地控制地圖的加載和初始化過程。該工具庫適用于各種前端框架和庫,如React、Vue、Angular等。
在項目中應(yīng)用:
1、控制臺安裝:
npm i amap-jsapi-loader --save
2、在main.js中配置秘鑰
//配置安全密鑰
window._AMapSecurityConfig = {
securityJsCode: '你的密鑰' //* 安全密鑰
}3、項目中創(chuàng)建MapView.vue文件用作地圖組件
4、在 MapView.vue 地圖組件中創(chuàng)建 div 標(biāo)簽作為地圖容器 ,并設(shè)置地圖容器的 id 屬性為 map;
<div class="content">
<!-- 用來顯示地圖 -->
<!-- 可以不寫寬度,但一定要有高度 -->
<div id="Map" style="height: 500px;">
</div>
</div>5、在地圖組件 MapView.vue 中引入 amap-jsapi-loader
import AMapLoader from '@amap/amap-jsapi-loader';
6、地圖初始化函數(shù) initMap,這里簡單實現(xiàn)了marker點標(biāo)記功能
<script>
//引入高德地圖
import AMapLoader from '@amap/amap-jsapi-loader';
export default {
name: 'IndexMap',
data() {
return {
map: null, //地圖實例
markerdom: null,
marker: [],
markernum: [
[108.925107, 34.238578],
[108.977807, 34.240636],
[108.977893, 34.20508],
[108.915065, 34.202951],
[108.920713, 34.226592],
......
//為了展示,用的寫好的數(shù)據(jù)
]
}
},
mounted() {
this.initMap();//調(diào)用地圖初始化函數(shù):mounted 函數(shù)會在 DOM 初始化完成后調(diào)用
},
methods: {
initMap() {
AMapLoader.load({
key: "", // 申請好的Web端開發(fā)者Key,首次調(diào)用 load 時必填
//2.0版本太卡了 ,所以使用的1.4.0版本 其插件也有不同 如:ToolBar
version: "1.4.0", // 指定要加載的 JSAPI 的版本,缺省時默認(rèn)為 1.4.15
resizeEnable: true,
plugins: [
"AMap.ToolBar", //工具條
"AMap.Scale", // 比例尺
"AMap.Geolocation", //定位
], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
}).then((AMap) => {
console.log(AMap);
this.map = new AMap.Map("Map", { //設(shè)置地圖容器id
resizeEnable: true,
rotateEnable: true,
pitchEnable: true,
zoom: 15,
pitch: 80,
rotation: -15,
viewMode: '3D',//開啟3D視圖,默認(rèn)為關(guān)閉
buildingAnimation: true,//樓塊出現(xiàn)是否帶動畫
expandZoomRange: true,
zooms: [3, 20],
center: [108.946651, 34.222718], //初始化地圖中心點位置
});
this.markerdom = '' +
'<div class="custom-content-marker">' +
' <img src="/icon_bike.png">' +
'</div>';
for (let i = 0; i < this.markernum.length; i++) {
this.marker.push(new AMap.Marker({
position: new AMap.LngLat(this.markernum[i][0], this.markernum[i][1]),// Marker經(jīng)緯度
content: this.markerdom, // 將 html 傳給 content
offset: new AMap.Pixel(-13, -30) // 以 icon 的 [center bottom] 為原點
}));
}
this.map.add(this.marker)
console.log(this.marker);
}).catch(e => {
console.log(e);
})
}
}
}
</script>看一下效果:

到此這篇關(guān)于vue集成高德地圖amap-jsapi-loader的實現(xiàn)的文章就介紹到這了,更多相關(guān)vue集成高德地圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue+Java+Base64實現(xiàn)條碼解析的示例
這篇文章主要介紹了Vue+Java+Base64實現(xiàn)條碼解析的示例,幫助大家實現(xiàn)條碼解析,感興趣的朋友可以了解下2020-09-09
vue-admin-template?動態(tài)路由的實現(xiàn)示例
本文主要介紹了ue-admin-template動態(tài)路由的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
Vue中commit和dispatch區(qū)別及用法辨析(最新)
在Vue中,commit和dispatch是兩個用于觸發(fā)Vuex store中的mutations和actions的方法,這篇文章主要介紹了Vue中commit和dispatch區(qū)別及其用法辨析,需要的朋友可以參考下2024-06-06
Vue3使用hook封裝媒體查詢和事件監(jiān)聽的代碼示例
這篇文章主要給大家詳細(xì)介紹Vue3如何使用hook封裝媒體查詢和事件監(jiān)聽,使得Vue的開發(fā)更加絲滑,文中通過代碼示例給大家介紹的非常詳細(xì),感興趣的同學(xué)跟著小編一起來學(xué)習(xí)吧2023-07-07
Vue無后端配合實現(xiàn)導(dǎo)出功能的示例代碼
這篇文章主要為大家詳細(xì)介紹了Vue如何在無后端配合的情況下實現(xiàn)導(dǎo)出功能,文中的示例代碼簡潔易懂,有需要的小伙伴可以跟隨小編一起了解一下2024-01-01

