百度地图搜索控件获取的点位不准
一. 问题讲解
我们在使用百度 2D 地图时,添加其搜索控件
<bm-control>
<bm-auto-complete v-model="workAddress" :sugStyle="{ zIndex: 999999 }" @confirm="handleConfirm">
<el-input v-model="workAddress" placeholder="请输入地址来直接查找相关位置" style="width:500px"></el-input>
</bm-auto-complete>
</bm-control>
当我们搜索位置,然后在中心点位标点(百度地图返回点位)
代码如下
handleConfirm({ item }) {
const that = this;
let queryString = `${item.value.city}${item.value.district}${item.value.business}`;
var myGeo = new BMap.Geocoder();
myGeo.getPoint(queryString, function (point) {
if (point) {
that.from.position.lng = point.lng;
that.from.position.lat = point.lat;
that.centerMap.lat = point.lat // 改变中心点位的坐标
that.centerMap.lng = point.lng;
let initFrom = {
path: [{ lng: that.centerMap.lng - 0.003468, lat: that.centerMap.lat + 0.002692 }, { lng: that.centerMap.lng - 0.006234, lat: that.centerMap.lat - 0.003139 }, {
lng: that.centerMap.lng + 0.001359,
lat: that.centerMap.lat + 0.000199
}],
gridName: "",
position: point,
backgroundColor: "#409EFF",
editing: true,
borderColor: "blue",
startTime: "",
endTime: "",
status: "1",
};
that.polygonPaths = []
that.from.path = initFrom.path
that.polygonPaths.push(initFrom);
that.polygonPaths[0].editing = true;
that.polygonPaths[0].borderColor = "blue";
that.polygonPaths[0].animation = "BMAP_ANIMATION_BOUNCE";
}
});
},
例如 我们搜索一个地铁站
点位跟标记位置明显是不一样的 开始我以为是点位标记有问题 我就先打印了一个此处搜索返回点位坐标
lat 为 30.603496083497404
lng 为 114.3097383216564
二. 解决方法
我这里换了本地搜索( local search)
代码如下
handleConfirm({ item }) {
const that = this;
let queryString = `${item.value.city}${item.value.district}${item.value.business}`;
function myFun() {
var point = local.getResults().getPoi(0).point; //获取第一个智能搜索的结果
console.log('经度:' + point.lng, '纬度:' + point.lat);
// 处理逻辑
}
var local = new BMap.LocalSearch(this.map, { //智能搜索
onSearchComplete: myFun
});
local.search(queryString)
},