使用Vue Baidu Map对百度地图实现输入框搜索定位
前端时间需要在页面的输入框输入地址,搜索并在百度地图上获取选定结果的坐标,前端使用了Vue + Element-ui,地图方面直接使用了封装好的百度地图vue组件-vue-baidu-map
输入框使用自动完成的Input组件,在输入地址时不断更新地图搜索结果和地图当前定位。
以下是demo代码
<template>
<div class="app-container">
<el-autocomplete
v-model="mapLocation.address"
:fetch-suggestions="querySearch"
placeholder="请输入详细地址"
style="width: 100%"
:trigger-on-focus="false"
@select="handleSelect"
/>
<div style="margin: 5px">
<baidu-map class="bm-view" :center="mapCenter" :zoom="mapZoom" :scroll-wheel-zoom="true" ak="baidu-ak" @ready="handlerBMap" />
</div>
</div>
</template> <script>
import BaiduMap from 'vue-baidu-map/components/map/Map.vue'
export default {
name: 'BaiduMapDemo',
components: {
BaiduMap
},
data() {
return {
mapZoom: 15,
mapCenter: { lng: 0, lat: 0 },
mapLocation: {
address: undefined,
coordinate: undefined
}
}
},
methods: {
handlerBMap({ BMap, map }) {
this.BMap = BMap
this.map = map
if (this.mapLocation.coordinate && this.mapLocation.coordinate.lng) {
this.mapCenter.lng = this.mapLocation.coordinate.lng
this.mapCenter.lat = this.mapLocation.coordinate.lat
this.mapZoom = 15
map.addOverlay(new this.BMap.Marker(this.mapLocation.coordinate))
} else {
this.mapCenter.lng = 113.271429
this.mapCenter.lat = 23.135336
this.mapZoom = 10
}
},
querySearch(queryString, cb) {
var that = this
var myGeo = new this.BMap.Geocoder()
myGeo.getPoint(queryString, function(point) {
if (point) {
that.mapLocation.coordinate = point
that.makerCenter(point)
} else {
that.mapLocation.coordinate = null
}
}, this.locationCity)
var options = {
onSearchComplete: function(results) {
if (local.getStatus() === 0) {
// 判断状态是否正确
var s = []
for (var i = 0; i < results.getCurrentNumPois(); i++) {
var x = results.getPoi(i)
var item = { value: x.address + x.title, point: x.point }
s.push(item)
cb(s)
}
} else {
cb()
}
}
}
var local = new this.BMap.LocalSearch(this.map, options)
local.search(queryString)
},
handleSelect(item) {
var { point } = item
this.mapLocation.coordinate = point
this.makerCenter(point)
},
makerCenter(point) {
if (this.map) {
this.map.clearOverlays()
this.map.addOverlay(new this.BMap.Marker(point))
this.mapCenter.lng = point.lng
this.mapCenter.lat = point.lat
this.mapZoom = 15
}
}
}
}
</script> <style>
.bm-view {
width: 100%;
height: 500px;
}
</style>
效果图
使用Vue Baidu Map对百度地图实现输入框搜索定位的更多相关文章
- vue Baidu Map --- vue百度地图插件
vue Baidu Map 官网:https://dafrok.github.io/vue-baidu-map/#/zh/start/installation javascript 官网:http:/ ...
- 如何利用【百度地图API】进行定位?非GPS定位
原文:如何利用[百度地图API]进行定位?非GPS定位 如果你可以上网,如果你有火狐浏览器,那么恭喜你.你能很容易使用以下代码进行定位! ------------------------------- ...
- 百度地图API地点搜索-获取经纬度
分享一下地图上的地点搜索和鼠标点击获取地点经纬度,这些都是地图比较基本和实用的代码,其中还包括了根据用户IP进行地图的显示.改变地图上的鼠标样式.启用滚轮缩放等,算是半入门吧,其他的一些可以自己参考百 ...
- HTML5调用百度地图API进行地理定位实例
自从HTML5的标准确定以后,越来越多的网站使用HTML5来进行开发.虽然对HTML5支持的浏览器不是很多,但是依然抵挡不了大伙对HTML5开发的热情.今天为大家带来的是使用HTML5调用百度地图AP ...
- Vue中使用百度地图——根据输入框输入的内容,获取详细地址
知识点:在Vue.js项目中调用百度地图API,实现input框,输入地址,在百度地图上定位到准确地址,获得到经纬度 参考博客: 百度地图的引用,初步了解参考博客:http://blog.csdn. ...
- vue项目中使用百度地图的方法
1.在百度地图申请密钥: http://lbsyun.baidu.com/ 将 <script type="text/javascript" src="http: ...
- vue 项目中引用百度地图
新建map.js export const BaiduMap = { init: function() { const BMapURL = 'https://api.map.baidu.com/api ...
- 在Vue框架中使用百度地图
1.首先在index.html中引入百度地图 <script type="text/javascript" src="http://api.map.baidu.co ...
- Vue Baidu Map 插件的使用
最近在做一个项目,技术采用的是Vue.js套餐,有个百度地图的需求,当时,大脑宕机,立马去引入百度地图API,当时想到两种方法,一种是在index.html中全局引入js,此法吾不喜,就采用了第二种异 ...
随机推荐
- 如何将Azure SQL 数据库还原到本地数据库实例中
原文:https://www.jerriepelser.com/blog/restore-sql-database-localdb/ 原文作者: Jerrie Pelser 译文:如何将Azure S ...
- SynchronusQueue
/** * 当向SynchronousQueue插入元素时,必须同时有个线程往外取 * SynchronousQueue是没有容量的,这是与其他的阻塞队列不同的地方 */ public class S ...
- C#上手练习7(构造方法语句)
创建类的对象是使用“类名 对象名 = new 类名()”的方式来实现的. 实际上,“类名()”的形式调用的是类的构造方法,也就是说构造方法的名字是与类的名称相同的. 构造方法的定义语法形式如下. 访问 ...
- 用 Python 自动监测 GitHub 项目更新
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: GitPython PS:如有需要Python学习资料的小伙伴可以加 ...
- Vuex细说
vuex 1,什么是 vuex? vuex 是一个专门为 vue.js 应用程序 开发的状态管理模式+库 它充当应用程序中所有组件的集中存储(数据状态) ,其规则确保状态只能以可预测的方式进行变更 并 ...
- JS基础语法---for循环遍历数组
for循环遍历数组 要显示数组中的每个数据,可以如下: var arr=[10,20,30,40,50]; //显示数组中的每个数据 console.log(arr[0]); console.log( ...
- Python用Pandas读写Excel
Pandas是python的一个数据分析包,纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具.Pandas提供了大量能使我们快速便捷地处理数据的函数和方法. Pandas官方文档 ...
- 如何在Appium中使用AI定位
当我们在写自动化测试脚本的时候,传统情况下一定要知道元素的属性,如id.name.class等.那么通过AI的方式定位元素可能就不需要知道元素的属性,评价人对元素的判断来定位,比如,看到一个搜索框,直 ...
- docker 安装 rabbitMQ服务器
1. 镜像拉取 拉取rabbitMQ镜像文件,后边要接上 management 表名是拉取带有web管理端的镜像,有web界面方便管理. 2.默认用户运行镜像 docker run -d -p 567 ...
- 3. 海思Hi3519A MPP从入门到精通(三 视频输入)
视频输入(VI)模块实现的功能:通过 MIPI Rx(含 MIPI 接口.LVDS 接口和 HISPI 接 口),SLVS-EC,BT.1120,BT.656,BT.601,DC 等接口接收视频数据. ...