vue用BMap百度地圖實(shí)現(xiàn)即時(shí)搜索功能
本文實(shí)例為大家分享了vue用BMap百度地圖實(shí)現(xiàn)即時(shí)搜索功能的具體代碼,供大家參考,具體內(nèi)容如下


功能如下:
搜索框搜索---自動下拉---點(diǎn)擊數(shù)據(jù)---數(shù)據(jù)顯示在搜索框里---點(diǎn)擊新增--數(shù)據(jù)顯示在下方--點(diǎn)擊刪除--刪除當(dāng)前
代碼:
首先去百度開發(fā)者申請一個(gè)key
然后將key引入到項(xiàng)目的 index.html:
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=你的key"></script>
下面是組件代碼:
<template>
<div id="app">
<el-form label-width="200px">
<el-form-item label="包含小區(qū)" required class="housing_input">
<el-input id="suggestId" v-model="city" placeholder="請輸入小區(qū)名稱" name="address_detail" />
<div id="allmap"/>
<el-button @click="add_housing">新增</el-button>
<div v-for="(item,index) in add_housing_list" :key="index" class="housingList">
<span>{{item}}</span>
<el-button class="delete_button" @click="delete_housing(index)">刪除</el-button>
</div>
</el-form-item>
</el-form>
</div>
</template>
<script>
export default {
name: 'demo',
data(){
return{
city: '',
address_detail: null, //詳細(xì)地址
add_housing_list: ["阿里巴巴"],
}
},
mounted() {
this.getcity()
},
methods:{
getcity(){
this.$nextTick(function() {
var th = this
// 創(chuàng)建Map實(shí)例
var map = new BMap.Map('allmap')
// 初始化地圖,設(shè)置中心點(diǎn)坐標(biāo),
var point = new BMap.Point(120.211877, 30.255194) // 創(chuàng)建點(diǎn)坐標(biāo),漢得公司的經(jīng)緯度坐標(biāo)
map.centerAndZoom(point, 15)
map.enableScrollWheelZoom()
var ac = new BMap.Autocomplete( // 建立一個(gè)自動完成的對象
{
'input': 'suggestId',
'location': map
})
var myValue
ac.addEventListener('onconfirm', function(e) { // 鼠標(biāo)點(diǎn)擊下拉列表后的事件
var _value = e.item.value //獲取點(diǎn)擊的條目
myValue = _value.province + _value.city + _value.district + _value.street + _value.business //地址拼接賦給一個(gè)變量
th.city = myValue //將地址賦給data中的city
// console.log(th.city)
setPlace()
})
// console.log(ac.pc.input)
function setPlace() {
map.clearOverlays() // 清除地圖上所有覆蓋物
function myFun() {
th.userlocation = local.getResults().getPoi(0).point // 獲取第一個(gè)智能搜索的結(jié)果
map.centerAndZoom(th.userlocation, 18)
map.addOverlay(new BMap.Marker(th.userlocation)) // 添加標(biāo)注
}
var local = new BMap.LocalSearch(map, { // 智能搜索
onSearchComplete: myFun
})
local.search(myValue)
// 測試輸出坐標(biāo)(指的是輸入框最后確定地點(diǎn)的經(jīng)緯度)
map.addEventListener('click', function(e) {
// 經(jīng)度
console.log(th.userlocation.lng)
// 緯度
console.log(th.userlocation.lat)
})
}
},)
},
// 新增小區(qū) 點(diǎn)擊的地址增加進(jìn)list
add_housing() {
this.add_housing_list.push(this.city)
},
// 刪除小區(qū)
delete_housing(index) {
// console.log(index)
this.add_housing_list.splice(index, 1)
},
}
}
</script>
<style scoped>
.housingList{
margin-top:20px;
}
.delete_button{
color: #409EFF;
text-decoration: underline;
border:none;
background:#fff;
cursor: pointer;
margin-left:20px;
}
.el-input{
width: 800px;
}
.housing_input .el-input{
width: 730px;
}
#allmap{
width: 400px;
height: 400px;
font-family: "微軟雅黑";
display: none;
}
</style>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vuex 實(shí)現(xiàn)getter值賦值給vue組件里的data示例
今天小編就為大家分享一篇vuex 實(shí)現(xiàn)getter值賦值給vue組件里的data示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
vue項(xiàng)目中如何使用video.js實(shí)現(xiàn)視頻播放與視頻進(jìn)度條打點(diǎn)
這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目中如何使用video.js實(shí)現(xiàn)視頻播放與視頻進(jìn)度條打點(diǎn)的相關(guān)資料,video.js是一款基于HTML5的網(wǎng)絡(luò)視頻播放器,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
詳解如何從零開始搭建Express+Vue開發(fā)環(huán)境
這篇文章主要介紹了詳解如何從零開始搭建Express+Vue開發(fā)環(huán)境,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07
詳解vue之自行實(shí)現(xiàn)派發(fā)與廣播(dispatch與broadcast)
這篇文章主要介紹了詳解vue之自行實(shí)現(xiàn)派發(fā)與廣播(dispatch與broadcast),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
詳解vuex中mutations方法的使用與實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了vuex中mutations方法的使用與實(shí)現(xiàn)的相關(guān)知識,文中的示例代碼簡潔易懂,具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起了解一下2023-11-11
vue項(xiàng)目watch內(nèi)的函數(shù)重復(fù)觸發(fā)問題的解決
這篇文章主要介紹了vue項(xiàng)目watch內(nèi)的函數(shù)重復(fù)觸發(fā)問題的兩種解決方案,幫助大家更好的理解和學(xué)習(xí)使用vue,感興趣的朋友可以了解下2021-04-04

