添加地图搜索 <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 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>onBaiduMapReady(e) { let that = this this.BMap = e.BMap; if (this.BMap) { const geolocation = new this.BMap.Geolocation(); geolocation.getCurrentPosition(res => { console.log(res) that.$nextTick(() => { that.loadingInstance.close(); }); that.center.lng = res.point.lng that.center.lat = res.point.lat const { province, city, district, street, street_number } = res.address; that.address = province + city + district + street + street_number; }) } },
时间: 2024-01-08 22:02:56 浏览: 120
这段代码使用了百度地图的 API 实现了一个地图搜索的功能。具体来说,它创建了一个弹窗对话框,并在其中添加了一个百度地图组件。这个组件包含一个地图视图和一个标记点,可以通过鼠标点击获取位置信息。
在 onBaiduMapReady 方法中,它首先获取当前位置信息,并将地图中心点设置为当前位置。然后,它将地址的各个部分(省、市、区、街道等)拼接在一起,赋值给 this.address 变量。最后,它关闭了一个 loading 实例。
在弹窗对话框的底部,它添加了一个输入框和两个按钮。输入框用于输入或选择收货地址,确定按钮用于确认选择的地址,取消按钮用于关闭弹窗对话框。
相关问题
<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>解释一下
这是一个使用了 Element UI 组件库中的 el-dialog 组件的 Vue 组件,其中:
- :title="title" 表示将父组件传入的 title 属性作为弹窗的标题;
- :visible.sync="open" 表示将父组件传入的 open 属性作为弹窗的显示状态,同时通过 .sync 修饰符实现双向绑定,使得父组件可以通过修改 open 属性来控制子组件的显示状态;
- width="500px" 表示设置弹窗的宽度为 500px;
- append-to-body 表示将弹窗的 DOM 节点插入到 body 元素上,而不是当前组件的父元素中。这样可以避免弹窗出现在某些滚动容器中时出现定位偏移的问题。
怎么添加一个地址搜索框: <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>
你可以使用百度地图的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>
```
阅读全文
相关推荐

















