欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue用BMap百度地圖實現(xiàn)即時搜索功能

 更新時間:2019年09月26日 11:37:33   作者:小羽向前跑  
這篇文章主要為大家詳細介紹了vue用BMap百度地圖實現(xiàn)即時搜索功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

功能如下:

搜索框搜索---自動下拉---點擊數(shù)據(jù)---數(shù)據(jù)顯示在搜索框里---點擊新增--數(shù)據(jù)顯示在下方--點擊刪除--刪除當前

代碼:

首先去百度開發(fā)者申請一個key

然后將key引入到項目的 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, //詳細地址
 add_housing_list: ["阿里巴巴"],
 }
 },
 mounted() {
 this.getcity()
 },
 methods:{
 getcity(){
 this.$nextTick(function() {
 var th = this
 // 創(chuàng)建Map實例
 var map = new BMap.Map('allmap')
 // 初始化地圖,設(shè)置中心點坐標,
 var point = new BMap.Point(120.211877, 30.255194) // 創(chuàng)建點坐標,漢得公司的經(jīng)緯度坐標
 map.centerAndZoom(point, 15)
 map.enableScrollWheelZoom()
 
 var ac = new BMap.Autocomplete( // 建立一個自動完成的對象
 {
  'input': 'suggestId',
  'location': map
 })
 var myValue
 ac.addEventListener('onconfirm', function(e) { // 鼠標點擊下拉列表后的事件
 var _value = e.item.value //獲取點擊的條目
 myValue = _value.province + _value.city + _value.district + _value.street + _value.business //地址拼接賦給一個變量
 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 // 獲取第一個智能搜索的結(jié)果
  map.centerAndZoom(th.userlocation, 18)
  map.addOverlay(new BMap.Marker(th.userlocation)) // 添加標注
 }
 
 var local = new BMap.LocalSearch(map, { // 智能搜索
  onSearchComplete: myFun
 })
 local.search(myValue)
 
 // 測試輸出坐標(指的是輸入框最后確定地點的經(jīng)緯度)
 map.addEventListener('click', function(e) {
  // 經(jīng)度
  console.log(th.userlocation.lng)
  // 緯度
  console.log(th.userlocation.lat)
 })
 }
 },)
 },
 // 新增小區(qū) 點擊的地址增加進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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論