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


功能如下:
搜索框搜索---自動(dòng)下拉---點(diǎn)擊數(shù)據(jù)---數(shù)據(jù)顯示在搜索框里---點(diǎn)擊新增--數(shù)據(jù)顯示在下方--點(diǎn)擊刪除--刪除當(dāng)前
代碼:
首先去百度開(kāi)發(fā)者申請(qǐng)一個(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ǐng)輸入小區(qū)名稱(chēng)" 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è)自動(dòng)完成的對(duì)象
{
'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)
// 測(cè)試輸出坐標(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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue實(shí)現(xiàn)input框禁止輸入標(biāo)簽
這篇文章主要介紹了vue實(shí)現(xiàn)input框禁止輸入標(biāo)簽,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue中 key keep-alive的實(shí)現(xiàn)原理
這篇文章主要介紹了Vue中 key keep-alive的實(shí)現(xiàn)原理,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
Vue項(xiàng)目啟動(dòng)報(bào)錯(cuò)解決方法大全
這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目啟動(dòng)報(bào)錯(cuò)解決方法的相關(guān)資料,文中通過(guò)圖文將解決的過(guò)程介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
Vue3中使用ref與reactive創(chuàng)建響應(yīng)式數(shù)據(jù)的示例代碼
這篇文章主要介紹了Vue3中使用ref與reactive創(chuàng)建響應(yīng)式數(shù)據(jù)的方法,文中通過(guò)代碼示例講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-08-08
Vue.js組件使用開(kāi)發(fā)實(shí)例教程
Vue.js的組件可以理解為預(yù)先定義好了行為的ViewModel類(lèi)。這篇文章主要介紹了Vue.js組件使用開(kāi)發(fā)實(shí)例教程的相關(guān)資料,需要的朋友可以參考下2016-11-11
Vue中$nextTick實(shí)現(xiàn)源碼解析
這篇文章主要為大家介紹了Vue中$nextTick實(shí)現(xiàn)源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
VUE?el-table列表搜索功能純前端實(shí)現(xiàn)方法
Vue表搜索是指在Vue應(yīng)用中實(shí)現(xiàn)對(duì)表格數(shù)據(jù)的搜索功能,下面這篇文章主要給大家介紹了關(guān)于VUE?el-table列表搜索功能純前端實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-09-09
Vue3.0實(shí)現(xiàn)圖片預(yù)覽組件(媒體查看器)功能
最近項(xiàng)目中有個(gè)場(chǎng)景,一組圖片、視頻、音頻、文件數(shù)據(jù),要求點(diǎn)擊圖片可以放大預(yù)覽,左右可以切換音視頻、文件,支持鼠標(biāo)及各種鍵控制?縮放,左右旋轉(zhuǎn),移動(dòng)等功能,這篇文章主要介紹了Vue3.0實(shí)現(xiàn)圖片預(yù)覽組件(媒體查看器),需要的朋友可以參考下2023-12-12

