vue項(xiàng)目中使用百度地圖的方法
1.在百度地圖申請密鑰: http://lbsyun.baidu.com/ 將
<script type="text/javascript" src=" 中的 密鑰替換成你申請的,在 vue項(xiàng)目的index.html引用。
2. 在build 文件下下的 webpack.base.conf.js貼入代碼
externals: {
"BMap": "BMap"
},

3. map.vue代碼(demo可以直接使用,demo使用了vue-clipboard2插件,請自行安裝)
<template>
<div>
<el-row >
<el-col :offset="2" :span="8">
<el-input :id="suggestId" v-model="address_detail" :clearable='clearable' placeholder="請輸入店鋪地址,獲取店鋪?zhàn)鴺?biāo)" >
</el-input>
</el-col>
<el-col :span="2">
<el-button id="position" @click="search" type="primary">定位</el-button>
</el-col>
<el-col :span="12" >
<el-tag type="success" v-clipboard:copy="userlocation.lng" v-clipboard:success="onCopy" v-clipboard:error="onError" >經(jīng)度 {{userlocation.lng}}</el-tag>
<el-tag type="success" v-clipboard:copy="userlocation.lat" v-clipboard:success="onCopy" v-clipboard:error="onError">緯度 {{userlocation.lat}}</el-tag>
<el-tag type="success" ><<<<點(diǎn)擊左側(cè)按鈕復(fù)制經(jīng)緯度信息</el-tag>
</el-col>
</el-row>
<el-row>
<el-col :offset="2" :span="18">
<div id="map_canvas" class="allmap"></div>
</el-col>
</el-row>
</div>
</template>
<script>
export default {
data() {
return {
address_detail: "", //詳細(xì)地址
userlocation: { lng: "", lat: "" },
clearable: true,
suggestId: "suggestId",
map : {},
mk: {}
};
},
created () {
},
methods: {
drawMap() {
this.map = new BMap.Map("map_canvas"); // 創(chuàng)建地圖實(shí)例
this.map.addControl(new BMap.NavigationControl()); // 啟用放大縮小 尺
this.map.enableScrollWheelZoom();
this.getlocation();//獲取當(dāng)前坐標(biāo), 測試時獲取定位不準(zhǔn)。
var point = new BMap.Point(this.userlocation.lng, this.userlocation.lat); // 創(chuàng)建點(diǎn)坐標(biāo)
this.map.centerAndZoom(point, 13); // 初始化地圖,設(shè)置中心點(diǎn)坐標(biāo)和地圖級別
var marker = new BMap.Marker(point); // 創(chuàng)建標(biāo)注
this.map.addOverlay(marker); // 將標(biāo)注添加到地圖中
var ac = new BMap.Autocomplete({
//建立一個自動完成的對象
input: "suggestId",
location: this.map
});
var myValue;
ac.addEventListener("onconfirm", (e)=> {
//鼠標(biāo)點(diǎn)擊下拉列表后的事件
var _value = e.item.value;
myValue =_value.province +_value.city +_value.district +_value.street +_value.business;
this.address_detail = myValue;
this.setPlace();
});
},
getMarker (point) {
this.mk = new BMap.Marker(point);
this.mk.addEventListener("dragend", this.showInfo);
this.mk.enableDragging(); //可拖拽
this.getAddress(point);
this.map.addOverlay(this.mk);//把點(diǎn)添加到地圖上
this.map.panTo(point);
},
getlocation () {
//獲取當(dāng)前位置
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition((r) =>{
if(geolocation.getStatus() == BMAP_STATUS_SUCCESS){
this.getMarker(r.point);
this.userlocation = r.point;
}else {
alert('failed'+this.getStatus());
}
});
},
//綁定Marker的拖拽事件
showInfo(e){
var gc = new BMap.Geocoder();
gc.getLocation(e.point, (rs)=>{
var addComp = rs.addressComponents;
var address = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;//獲取地址
//畫圖 ---》顯示地址信息
var label = new BMap.Label(address,{offset:new BMap.Size(20,-10)});
this.map.removeOverlay(this.mk.getLabel());//刪除之前的label
this.mk.setLabel(label);
this.address_detail = address;
this.userlocation = e.point;
});
},
//獲取地址信息,設(shè)置地址label
getAddress(point){
var gc = new BMap.Geocoder();
gc.getLocation(point, (rs)=>{
var addComp = rs.addressComponents;
var address = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;//獲取地址
//畫圖 ---》顯示地址信息
var label = new BMap.Label(address,{offset:new BMap.Size(20,-10)});
this.map.removeOverlay(this.mk.getLabel());//刪除之前的label
this.address_detail = address;
this.mk.setLabel(label);
});
},
setPlace() {
this.map.clearOverlays(); //清除地圖上所有覆蓋物
var th = this
function myFun() {
th.userlocation = local.getResults().getPoi(0).point; //獲取第一個智能搜索的結(jié)果
th.map.centerAndZoom(th.userlocation, 18);
th.getMarker(th.userlocation);
}
var local = new BMap.LocalSearch(this.map, {
onSearchComplete: myFun //智能搜索
});
local.search(this.address_detail);
},
search () {
var localSearch = new BMap.LocalSearch(this.map);
localSearch.enableAutoViewport(); //允許自動調(diào)節(jié)窗體大小
this.searchByInputName(localSearch);
},
searchByInputName(localSearch) {
this.map.clearOverlays(); //清空原來的標(biāo)注
var th = this;
var keyword = this.address_detail;
localSearch.setSearchCompleteCallback(function(searchResult) {
var poi = searchResult.getPoi(0);
th.userlocation = poi.point;
th.map.centerAndZoom(poi.point, 13);
th.getMarker(th.userlocation);
});
localSearch.search(keyword);
},
onCopy () {
this.$message('內(nèi)容已復(fù)制到剪貼板!');
},
onError () {
this.$message('內(nèi)容復(fù)制失敗,請重試!');
}
},
mounted() {
this.$nextTick(function() {
this.drawMap();
});
}
};
</script>
<style scoped>
.allmap {
width: 100%;
height: 400px;
font-family: "微軟雅黑";
border: 1px solid green;
}
.el-tag {
cursor: pointer;
}
</style>
總結(jié)
以上所述是小編給大家介紹的vue項(xiàng)目中使用百度地圖的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Vue3項(xiàng)目中使用防抖節(jié)流的實(shí)現(xiàn)示例
防抖節(jié)流是可以說是一種優(yōu)化組件性能的技巧,可以有效減少組件中的渲染次數(shù)和計(jì)算量,本文主要介紹了Vue3項(xiàng)目中使用防抖節(jié)流的實(shí)現(xiàn)示例,感興趣的可以了解一下2024-04-04
vue中 數(shù)字相加為字串轉(zhuǎn)化為數(shù)值的例子
今天小編就為大家分享一篇vue中 數(shù)字相加為字串轉(zhuǎn)化為數(shù)值的例子,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
在antd Form表單中select設(shè)置初始值操作
這篇文章主要介紹了在antd Form表單中select設(shè)置初始值操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
vue3中頁面跳轉(zhuǎn)兩種實(shí)現(xiàn)方式
在Vue3中Vue?Router是一個常用的路由管理庫,它提供了一種簡單而強(qiáng)大的方式來實(shí)現(xiàn)路由跳轉(zhuǎn)和導(dǎo)航,這篇文章主要給大家介紹了關(guān)于vue3中頁面跳轉(zhuǎn)的兩種實(shí)現(xiàn)方式,需要的朋友可以參考下2024-09-09

