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)
}
}
});
},
```
uniapp 百度地图 拖动获取经纬度级搜索连用
最新推荐文章于 2024-06-15 19:17:30 发布