Vue2中百度地圖API的使用

1,前言


記錄一下Vue2項目中,百度地圖API的簡單使用。

2,申請賬號,獲取key


需要先申請百度賬號,然后登陸百度地圖開放平臺:https://lbsyun.baidu.com/index.php?title=jspopularGL/guide/getkey,按照流程,申請成為開發(fā)者,然后創(chuàng)建應(yīng)用。

申請

應(yīng)用類型選擇瀏覽器端,Referer白名單填*

創(chuàng)建項目

3,安裝依賴


npm i --save vue-baidu-map

4,全局引入用法


先在main.js中引入

import BaiduMap from 'vue-baidu-map'
Vue.use(BaiduMap, { ak: '你申請的key' })

然后在你的.vue文件中

<template>
    <div id="index">
        <baidu-map class="map" :center="center" :zoom="zoom" @ready="handler" />
    </div>
</template>

<script>
export default {
    name: 'Index',
    components: {},
    data() {
        return {
            center: { lng: 0, lat: 0 },
            zoom: 0
        }
    },
    created() {},
    mounted() {},
    methods: {
        handler({ BMap, map }) {
            console.log(BMap, map)
            this.center.lng = 121.487899486
            this.center.lat = 31.24916171
            this.zoom = 15
        }
    }
}
</script>

<style lang="less" scoped>
.map{
    width: 500px;
    height: 500px;
}
</style>

沒問題的話現(xiàn)在已經(jīng)可以在頁面上看到上海市了

5,局部引入用法


在你的.vue文件中

<template>
    <div id="index">
        <baidu-map class="map" :ak="ak" :center="center" :zoom="zoom" @ready="handler" />
    </div>
</template>

<script>
import BaiduMap from 'vue-baidu-map/components/map/Map.vue'

export default {
    name: 'Index',
    components: {
        BaiduMap
    },
    data() {
        return {
            ak: '你的key',
            center: { lng: 0, lat: 0 },
            zoom: 0
        }
    },
    created() {},
    mounted() {},
    methods: {
        handler({ BMap, map }) {
            console.log(BMap, map)
            this.center.lng = 121.487899486
            this.center.lat = 31.24916171
            this.zoom = 15
        }
    }
}
</script>

<style lang="less" scoped>
.map{
    width: 500px;
    height: 500px;
}
</style>

6,常用參數(shù)說明&文檔


屬性

屬性名 類型 默認(rèn)值 描述
ak String 百度地圖開發(fā)者平臺申請的密鑰,僅在局部注冊組件時聲明
center Point, String 定位, 可使用如"廣州市海珠區(qū)"的地區(qū)字符串,也可以使用對象如 {lng: 116.404, lat: 39.915} 表示經(jīng)緯度
zoom Number 縮放等級
min-zoom Number 最小縮放級別
max-zoom Number 最大縮放級別
map-click Boolean true 允許點擊 該項僅在地圖組件掛載時加載一次
dragging Boolean true 允許拖拽
scroll-wheel-zoom Boolean true 允許鼠標(biāo)滾輪縮放

事件

事件名 參數(shù) 描述
click {type, target, point, pixel, overlay} 左鍵單擊地圖時觸發(fā)此事件。 當(dāng)雙擊時,產(chǎn)生的事件序列為: click click dblclick
dblclick {type, target, pixel, point} 鼠標(biāo)雙擊地圖時會觸發(fā)此事件
dragstart {type, target, pixel, point} 開始拖拽地圖時觸發(fā)
dragging {type, target, pixel, point} 拖拽地圖過程中觸發(fā)
dragend {type, target, pixel, point} 停止拖拽地圖時觸發(fā)
resize {type, target, size} 地圖可視區(qū)域大小發(fā)生變化時會觸發(fā)此事件
hotspotclick {type, target, spots} 點擊熱區(qū)時觸發(fā)此事件
hotspotover {type, target, spots} 鼠標(biāo)移至熱區(qū)時觸發(fā)此事件
hotspotout {type, target, spots} 鼠標(biāo)移出熱區(qū)時觸發(fā)此事件
tilesloaded {type, target} 當(dāng)?shù)貓D所有圖塊完成加載時觸發(fā)此事件

官方文檔:https://dafrok.github.io/vue-baidu-map/#/zh/index


如果看了覺得有幫助的,我是@鵬多多11997110103,歡迎 點贊 關(guān)注 評論;
END

往期文章

個人主頁

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容