怎么在vue中使用BMap百度地图实现一个即时搜索功能?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
Vue具体轻量级框架、简单易学、双向数据绑定、组件化、数据和结构的分离、虚拟DOM、运行速度快等优势,Vue中页面使用的是局部刷新,不用每次跳转页面都要请求所有数据和dom,可以大大提升访问速度和用户体验。
具体内容如下
首先去百度开发者申请一个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="包含小区" required class="housing_input">
<el-input id="suggestId" v-model="city" placeholder="请输入小区名称" 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
// 创建Map实例
var map = new BMap.Map('allmap')
// 初始化地图,设置中心点坐标,
var point = new BMap.Point(120.211877, 30.255194) // 创建点坐标,汉得公司的经纬度坐标
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 // 获取第一个智能搜索的结果
map.centerAndZoom(th.userlocation, 18)
map.addOverlay(new BMap.Marker(th.userlocation)) // 添加标注
}
var local = new BMap.LocalSearch(map, { // 智能搜索
onSearchComplete: myFun
})
local.search(myValue)
// 测试输出坐标(指的是输入框最后确定地点的经纬度)
map.addEventListener('click', function(e) {
// 经度
console.log(th.userlocation.lng)
// 纬度
console.log(th.userlocation.lat)
})
}
},)
},
// 新增小区 点击的地址增加进list
add_housing() {
this.add_housing_list.push(this.city)
},
// 删除小区
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>
看完上述内容,你们掌握怎么在vue中使用BMap百度地图实现一个即时搜索功能的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。