怎么添加一个地址搜索框: <el-dialog id="myMapDialog" title="请选择收货地址" width="500px" :visible.sync="dialogVisible" right=0> <baidu-map class="map" :center="center" :zoom="zoom" :scroll-wheel-zoom="true" @ready="onBaiduMapReady" @click="getClickInfo"> <!-- :zoom 缩放级别; :scroll-wheel-zoom:是否缩放; @ready: 地图组件加载完成后触发的事件; @click: 点击地图时触发的事件。 --> <!-- 展示地图 --> <bm-view style="width: 100%; height: 100%;" /> <!-- 添加标记点 --> <bm-marker :position="{ lng: center.lng, lat: center.lat }" :dragging="true" animation="BMAP_ANIMATION_BOUNCE" /> <!-- 显示当前位置 anchor 控件的停靠位置 ;:showAddressBar 显示地址栏 :autoLocation 自动定位--> <bm-geolocation anchor="BMAP_ANCHOR_BOTTOM_RIGHT" :showAddressBar="true" :autoLocation="true"></bm-geolocation> </baidu-map> <div slot="footer"> <el-input v-model="address" style="margin-bottom: 15px;" placeholder="请选择或填写收货地址"></el-input> <el-button type="primary" @click="getAddr">确定</el-button> <el-button @click="dialogVisible = false">取消</el-button> </div> </el-dialog>
时间: 2023-10-06 11:06:40 浏览: 110
下拉框的选项中添加搜索框
3星 · 编辑精心推荐
你可以使用百度地图的API来实现一个地址搜索框。以下是一个简单的实现方式:
1. 在 el-input 上添加一个 ref 属性,例如: `<el-input ref="searchBox" v-model="address" style="margin-bottom: 15px;" placeholder="请选择或填写收货地址"></el-input>`
2. 在 baidu-map 组件的 ready 事件中获取搜索框的实例,并添加监听事件。代码如下:
```
onBaiduMapReady(map) {
const searchBox = this.$refs.searchBox.$el.querySelector('input'); // 获取搜索框实例
const local = new BMap.LocalSearch(map, {
onSearchComplete: (results) => {
if (local.getStatus() === BMAP_STATUS_SUCCESS) {
const point = results.getPoi(0).point;
map.centerAndZoom(point, 18);
}
}
});
searchBox.addEventListener('keydown', (event) => {
if (event.keyCode === 13) { // 监听回车键
const keyword = event.target.value.trim();
if (keyword) {
local.search(keyword);
}
}
});
},
```
3. 在方法 getAddr 中,你可以直接使用 this.address 获取到搜索框中的内容。
完整代码如下:
```
<template>
<el-dialog id="myMapDialog" title="请选择收货地址" width="500px" :visible.sync="dialogVisible" right=0>
<baidu-map class="map" :center="center" :zoom="zoom" :scroll-wheel-zoom="true" @ready="onBaiduMapReady" @click="getClickInfo">
<bm-view style="width: 100%; height: 100%;" />
<bm-marker :position="{ lng: center.lng, lat: center.lat }" :dragging="true" animation="BMAP_ANIMATION_BOUNCE" />
<bm-geolocation anchor="BMAP_ANCHOR_BOTTOM_RIGHT" :showAddressBar="true" :autoLocation="true"></bm-geolocation>
</baidu-map>
<div slot="footer">
<el-input ref="searchBox" v-model="address" style="margin-bottom: 15px;" placeholder="请选择或填写收货地址"></el-input>
<el-button type="primary" @click="getAddr">确定</el-button>
<el-button @click="dialogVisible = false">取消</el-button>
</div>
</el-dialog>
</template>
<script>
import BaiduMap from 'vue-baidu-map';
export default {
components: { BaiduMap },
data() {
return {
center: { lng: 116.404, lat: 39.915 },
zoom: 15,
dialogVisible: true,
address: '',
};
},
methods: {
onBaiduMapReady(map) {
const searchBox = this.$refs.searchBox.$el.querySelector('input');
const local = new BMap.LocalSearch(map, {
onSearchComplete: (results) => {
if (local.getStatus() === BMAP_STATUS_SUCCESS) {
const point = results.getPoi(0).point;
map.centerAndZoom(point, 18);
}
}
});
searchBox.addEventListener('keydown', (event) => {
if (event.keyCode === 13) {
const keyword = event.target.value.trim();
if (keyword) {
local.search(keyword);
}
}
});
},
getClickInfo(event) {
this.center = { lng: event.point.lng, lat: event.point.lat };
},
getAddr() {
console.log(this.address); // 获取搜索框内容
},
},
};
</script>
```
阅读全文