uniapp 百度地图 拖动获取经纬度级搜索连用

import loadBMap from '@/utils/loadBMap.js'// 百度聚合具体代码
// 拖动
initMapc() {
				let that = this
				loadBMap('百度key').then(() => {
					map = new BMap.Map('mapContainer')
					const centerPoint = new BMap.Point(this.longitude, this.latitude)
					map.centerAndZoom(centerPoint, this.zoom)
					// 添加缩放控件
					map.addControl(new BMap.NavigationControl());
					// 创建标记
					var isDragging = false;
					var timer = null;
					marker = new BMap.Marker(centerPoint);
					// 将标记添加到地图中
					map.addOverlay(marker);
					// map.panTo(position.point, 15) //让指定位置移动到地图中心
					// 监听地图拖动事件
					var isDragging = false;
					var timer = null;
					map.addEventListener('dragend', () => {
						isDragging = true;
						// 如果已经设置了一个定时器,则清除它
						if (timer) {
							clearTimeout(timer);
						}
						// 创建一个新的定时器,延迟一段时间后执行
						timer = setTimeout(function() {
							if (isDragging) {
								// 如果仍然处于拖动状态,则认为拖动已经停止
								isDragging = false;

								const newCenter = map.getCenter();
								marker.setPosition(newCenter); // 更新标记点位置
								// console.log(newCenter)
								// console.log(newCenter.lng )
								that.latitude = newCenter.lat
								that.longitude = newCenter.lng
								that.transLocation()
							}
						}, 300); // 设置延迟时间,单位为毫秒

					});
				})
			},
			// 搜索
			loadAddress() {
				let that = this
				that.infor.adcode = ''
				that.list = []
				var local = new BMap.LocalSearch(map, {});
				local.enableAutoViewport(); // 启用自动视野定位
				// 监听搜索结果
				let poiOne = null
				local.search(this.searchValue); // this.searchValue 搜索文字
				local.setSearchCompleteCallback(function(searchResult) {
					for (let i = 0; i < searchResult.getNumPois(); i++) {
						let poi = searchResult.getPoi(i);
						if (searchResult.getPoi(0)) {
							poiOne = searchResult.getPoi(0)
							const centerPoint = new BMap.Point(poiOne.point.lng, poiOne.point.lat)
							map.centerAndZoom(centerPoint, that.zoom)
							// 更新地图中心为标记点位置
							map.panTo(new BMap.Point(poiOne.point.lng, poiOne.point.lat));
						}
						if (typeof(poi) != 'undefined' || poi) {
							that.list.push(poi)
						}

					}
				});
			},
			```

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值