elementUI输入框/选项卡与百度地图联动

文章目录

输入框/选项卡与百度地图联动

示例

输入框/选项卡与百度地图联动

html部分

javascript 复制代码
<template>
	<el-form ref="Froms" :model="formData" label-width="120px">
		<el-form-item label="经营地区:" prop="businessArea">
			<v-region-group
	             :town="false"
	             v-model="regionArea"
	             @change="regionChange"
	           >
	         </v-region-group>
	      </el-form-item>
	      <el-form-item label="详细地址:" prop="shopAddrExt">
	        <el-input v-model="formData.shopAddrExt" placeholder="请输入详细地址" size="small" @input="inputShopAddr" />
	        <div class="map">
                <baidu-map 
                  @ready="initMap"
                  @click="getLocation"
                  :center="center"
                   class="map"
                  :zoom="zoom"
                  :scroll-wheel-zoom="true"
                >
                <bm-navigation anchor="BMAP_ANCHOR_TOP_RIGHT" />
                </baidu-map>
            </div>
          </el-form-item>
	</el-form>
</template>

<script>
	export default {
		data(){
			return{
				formData: {
					shopAddrExt: ''
				},
				regionArea: {}, // 所选区域
		        center: { lng: 0, lat: 0 }, // 坐标
		        centerPoint: {},
		        zoom: 3, 
				BMap: null,
			    map: null,
			    geoCoder: null // 地址解析器
			}
		},
		methods: {
			initMap({ BMap, map }) {
				this.zoom = 15;
      			this.BMap = BMap;
      			this.map = map;
      			this.geoCoder = new BMap.Geocoder(); // 创建地址解析器的实例
      			let geolocation = new BMap.Geolocation(); // 获取当前经纬度用作地图显示中心点
      			geolocation.getCurrentPosition((e)=>{ // 方法获取设备在当前所处的坐标位置
      				console.log(e);
      				this.center.lng = e.longitude;
      				this.center.lat = e.latitude;
      				this.setCenterZoom(e); // e获取到经纬度设置地图中心点
      				this.setIcon(e); // 创建点坐标
      			})
      			// 监听地图缩放结束事件 lng表示经度,lat表示纬度(处理百度地图放大之后中心点偏移问题)
      			this.map.addEventListener('zoomend', (e)=> {
      				this.map.centerAndZoom(
      					new BMap.Point(this.center.lng, this.center.lat),
      					this.map.getZoom();
      				);
      			});
      			// enableMapClick:false 表示禁止地图内景点信息点击
      			// this.map = new BMap.Map('baiduMap', { enableMapClick: false });
      			// 设置地图允许的最大最小级别
      			// this.map.setMinZoom(5);
      			// this.map.setMaxZoom(20);
      			// 开启鼠标滚轮缩放
      			// this.map.enableScrollWheelZoom(true);
			},
			// 设置显示中心点
			setCenterZoom(e){
				this.center.lng = e.longitude;
				this.center.lat = e.latitude;
				this.centerPoint = new BMap.Point(e.longitude, e.latitude); // 选择一个经纬度作为中心点
				this.map.centerAndZoom(this.centerPoint, 14); // 设置地图中心点和缩放级别
			},
			// 创建点坐标
			setIcon(latLng){
      			// 创建自定义标记 参数:自定义图片路径 大小 偏移量
      			const icon = new BMap.Icon(
       			 	require('../../../../../static/icon/position4.png'),
       			 	new BMap.Size(32, 32),
			        // { anchor: new BMap.Size(0, 0) }
			     );
			     // 生成自定义图标点
			     // 创建点
			     const point = new BMap.Point(latLng.longitude, latLng.latitude);
			     // 创建标注
			     const marker = new BMap.Marker(point, { icon: icon });
			     this.map.clearOverlays(); // 先删除
			     this.map.addOverlay(marker);// 将标注添加到地图中
			     // 给标记点添加点击事件
			     marker.addEventListener('click', (e) => {
			     	// 执行想进行的操作(经个人测试在此处注册点击事件效果最佳, 该有的数据项都可以获取)
			     	console.log(e)
			     })
      		},
      		// 获取地图点击的地址
      		getLocation(e){
      			// console.log(e.point)
      			let latLng = {
			        longitude: e.point.lng,
			        latitude: e.point.lat
			    }
			    this.setCenterZoom(latLng);// 更改地图显示中心点
			    this.setIcon(latLng);// 创建点坐标
			    this.geoCoder.getLocation(e.point, (rs) => {
			    	// console.log(rs.surroundingPois) // 附近的POI点(array) 
			    	// console.log(rs.business) // 商圈字段
			    	let adr = rs.addressComponents;
			    	let address = adr.province + adr.city + adr.district + adr.street + adr.streetNumber; // 省市区街道门牌号
			    	this.formData.shopAddrExt = address; // 给input框赋值
			    });
      		},
      		// 根据输入的地址定位获取当前经纬度/根据当前地址获取经纬度
      		inputShopAddr(inputValue){
      			this.geoCoder.getPoint(inputValue, (point) => {
      				let latLng = {
			          longitude: point.lng,
			          latitude: point.lat
			        }
			        this.setCenterZoom(latLng);// 更改地图显示中心点
			        this.setIcon(latLng);// 创建点坐标
      			})
      		},
      		// 选择经营地区
      		regionChange (data) {
      			console.log(data);
      			let province = data.province ? data.province.value : '';
      			let city = data.city ? data.city.value : '';
      			let area = data.area ? data.area.value : '';
      			this.formData.businessArea = province + city + area;
      			this.inputShopAddr(this.formData.businessArea);
      		}
		}
	}
</script>

用到的插件

javascript 复制代码
// v-region插件
npm i v-region
// 百度地图
npm install vue-baidu-map --save

main.js

javascript 复制代码
// v-region插件
import Region from 'v-region'
Vue.use(Region);

// 百度地图
import BaiduMap from 'vue-baidu-map'
Vue.use(BaiduMap, {
  ak: '************************************'
})
相关推荐
JUNAI_Strive_ving16 分钟前
番茄小说逆向爬取
javascript·python
看到请催我学习25 分钟前
如何实现两个标签页之间的通信
javascript·css·typescript·node.js·html5
twins352044 分钟前
解决Vue应用中遇到路由刷新后出现 404 错误
前端·javascript·vue.js
qiyi.sky1 小时前
JavaWeb——Vue组件库Element(3/6):常见组件:Dialog对话框、Form表单(介绍、使用、实际效果)
前端·javascript·vue.js
煸橙干儿~~1 小时前
分析JS Crash(进程崩溃)
java·前端·javascript
哪 吒1 小时前
华为OD机试 - 几何平均值最大子数(Python/JS/C/C++ 2024 E卷 200分)
javascript·python·华为od
安冬的码畜日常1 小时前
【D3.js in Action 3 精译_027】3.4 让 D3 数据适应屏幕(下)—— D3 分段比例尺的用法
前端·javascript·信息可视化·数据可视化·d3.js·d3比例尺·分段比例尺
l1x1n02 小时前
No.3 笔记 | Web安全基础:Web1.0 - 3.0 发展史
前端·http·html
Q_w77422 小时前
一个真实可用的登录界面!
javascript·mysql·php·html5·网站登录
昨天;明天。今天。2 小时前
案例-任务清单
前端·javascript·css