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

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

示例

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

html部分

<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>

用到的插件

// v-region插件
npm i v-region
// 百度地图
npm install vue-baidu-map --save

main.js

// v-region插件
import Region from 'v-region'
Vue.use(Region);

// 百度地图
import BaiduMap from 'vue-baidu-map'
Vue.use(BaiduMap, {
  ak: '************************************'
})

相关推荐

  1. elementUI输入/选项地图联动

    2024-06-07 11:10:08       36 阅读
  2. vue地图的和element输入/v-region的联动

    2024-06-07 11:10:08       54 阅读
  3. 地图瓦片下载地址

    2024-06-07 11:10:08       34 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-07 11:10:08       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-07 11:10:08       101 阅读
  3. 在Django里面运行非项目文件

    2024-06-07 11:10:08       82 阅读
  4. Python语言-面向对象

    2024-06-07 11:10:08       91 阅读

热门阅读

  1. 堆(Heap)和栈(Stack)

    2024-06-07 11:10:08       30 阅读
  2. Python 脚本打包

    2024-06-07 11:10:08       27 阅读
  3. 二叉树的镜像--c++【做题记录】

    2024-06-07 11:10:08       35 阅读
  4. 使用C++版本的opencv dnn 部署onnx模型

    2024-06-07 11:10:08       31 阅读
  5. 【OpenCV 基础知识 21】霍夫变换圆形检测

    2024-06-07 11:10:08       28 阅读
  6. Ubuntu系统中挂载一个jar运行程序

    2024-06-07 11:10:08       30 阅读
  7. flink 作业动态维护更新,不重启flink,不提交作业

    2024-06-07 11:10:08       34 阅读
  8. Flink协调器Coordinator及自定义Operator

    2024-06-07 11:10:08       23 阅读
  9. 人工智能安全风险分析及应对策略

    2024-06-07 11:10:08       30 阅读