前端时间需要在页面的输入框输入地址,搜索并在百度地图上获取选定结果的坐标,前端使用了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对百度地图实现输入框搜索定位的更多相关文章

  1. vue Baidu Map --- vue百度地图插件

    vue Baidu Map 官网:https://dafrok.github.io/vue-baidu-map/#/zh/start/installation javascript 官网:http:/ ...

  2. 如何利用【百度地图API】进行定位?非GPS定位

    原文:如何利用[百度地图API]进行定位?非GPS定位 如果你可以上网,如果你有火狐浏览器,那么恭喜你.你能很容易使用以下代码进行定位! ------------------------------- ...

  3. 百度地图API地点搜索-获取经纬度

    分享一下地图上的地点搜索和鼠标点击获取地点经纬度,这些都是地图比较基本和实用的代码,其中还包括了根据用户IP进行地图的显示.改变地图上的鼠标样式.启用滚轮缩放等,算是半入门吧,其他的一些可以自己参考百 ...

  4. HTML5调用百度地图API进行地理定位实例

    自从HTML5的标准确定以后,越来越多的网站使用HTML5来进行开发.虽然对HTML5支持的浏览器不是很多,但是依然抵挡不了大伙对HTML5开发的热情.今天为大家带来的是使用HTML5调用百度地图AP ...

  5. Vue中使用百度地图——根据输入框输入的内容,获取详细地址

    知识点:在Vue.js项目中调用百度地图API,实现input框,输入地址,在百度地图上定位到准确地址,获得到经纬度 参考博客:  百度地图的引用,初步了解参考博客:http://blog.csdn. ...

  6. vue项目中使用百度地图的方法

    1.在百度地图申请密钥: http://lbsyun.baidu.com/  将 <script type="text/javascript" src="http: ...

  7. vue 项目中引用百度地图

    新建map.js export const BaiduMap = { init: function() { const BMapURL = 'https://api.map.baidu.com/api ...

  8. 在Vue框架中使用百度地图

    1.首先在index.html中引入百度地图 <script type="text/javascript" src="http://api.map.baidu.co ...

  9. Vue Baidu Map 插件的使用

    最近在做一个项目,技术采用的是Vue.js套餐,有个百度地图的需求,当时,大脑宕机,立马去引入百度地图API,当时想到两种方法,一种是在index.html中全局引入js,此法吾不喜,就采用了第二种异 ...

随机推荐

  1. iOS:捋一遍View的生命周期

    一.介绍 前面介绍了VC的生命周期,闲着没事也来捋一捋View的生命周期,简单用两个类型的View来监测.一个View纯代码创建,另一个View使用Xib创建. 二 .代码 MyCodeView:  ...

  2. php date获取前一天的时间

    结果: 结论: 第二种方式只使用了一个函数,所以更快一些,速度大约是第一种的两倍

  3. 《细说PHP》第四版 样章 第二章 PHP的应用与发展 1

    <细说PHP>第四版 样章 第二章 PHP的应用与发展 1 学习任何编程语言之前,先了解一下它的应用与发展是很有必要的.从Web开发的历史看来,PHP.Python和Ruby几乎是同时出现 ...

  4. 创 PHP RSA2 签名算法

        什么是RSA2 ? RSA2 是在原来SHA1WithRSA签名算法的基础上,新增了支持SHA256WithRSA的签名算法. 该算法比SHA1WithRSA有更强的安全能力. 为了您的应用安 ...

  5. 来认识一下venus-init——一个让你仅需一个命令开始Java开发的命令行工具

    源代码地址: Github仓库地址 个人网站:个人网站地址 前言 不知道你是否有过这样的经历.不管你是什么岗位,前端也好,后端也罢,想去了解一下Java开发到底是什么样的,它是不是真的跟传说中的一样. ...

  6. 关于excel中的vlookup就是查找当前列对应的下一列的值的使用

    关于excel中的vlookup就是查找当前列对应的下一列的值的使用 vlookup的使用一些说明 vlookup函数一个4个参数解释下 vlookup(查找的值,表格范围,表格范围中第几列的值,0是 ...

  7. 安装Keepalived namespaces.c:187: error: ‘SYS_setns’ undeclared (first use in this function)

    错误信息 namespaces.c: In function ‘setns’: namespaces.c:: error: ‘SYS_setns’ undeclared (first use in t ...

  8. Python笔记:设计模式之工厂模式

    工厂模式:“工厂”即表示一个负责创建其他类型的对象的类,通常情况下,一个工厂的对象会有一个或多个方法与之关联,这些方法用于创建不同类型的对象,工厂对象会根据客户端给方法传递的不同的参数或者客户端调用不 ...

  9. Linux软件安装——服务管理

    Linux软件安装——服务管理 摘要:本文主要学习了Linux中有关服务管理的知识. 什么是服务 服务一般是放置在后台运行的一个或多个进分程,为用户或系统提供某项特定的服务,有些是系统服务,有些则是独 ...

  10. FCC---Create a More Complex Shape Using CSS and HTML---一个粉色爱心

    One of the most popular shapes in the world is the heart shape, and in this challenge you'll create ...