Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit caa4681

Browse files
committed
百度地图搜索/定位
1 parent d9d11a5 commit caa4681

File tree

2 files changed

+25
-34
lines changed

2 files changed

+25
-34
lines changed

src/component/baiduMap.vue

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
append-to-body
77
draggable
88
destroy-on-close>
9-
经纬度:[{{ lnglat }}]
10-
<div id="container" style="width: 100%; height: 400px; background: #fafafa"/>
9+
经纬度:[{{ lng + ',' + lat }}]
10+
<div id="container" style="height: 400px"/>
1111
</el-dialog>
1212
</template>
1313

@@ -19,39 +19,30 @@ const showDialog = ref(false);
1919
const dialogTitle = ref('');
2020
const lng = ref('');
2121
const lat = ref('');
22-
const lnglat = ref('');
23-
const initMap = (coord, address) => {
22+
const initMap = (address) => {
2423
showDialog.value = true;
2524
dialogTitle.value = address;
26-
coord = coord ? coord : '116.404,39.915';
27-
lnglat.value = coord;
28-
if (coord) {
29-
let arr = coord.split(',');
30-
lng.value = arr[0];
31-
lat.value = arr[1];
32-
nextTick(() => {
33-
const map = new BMapGL.Map('container');
34-
map.enableScrollWheelZoom(true); //滚轮缩放
35-
map.addControl(new BMapGL.CityListControl()); //添加城市列表控件
36-
map.addControl(new BMapGL.ZoomControl()); //添加缩放控件
37-
//创建地址解析器实例
38-
const myGeo = new BMapGL.Geocoder();
39-
//将地址解析结果显示在地图上,并调整地图视野
40-
myGeo.getPoint(address, (point) => {
41-
console.log('point', point);
42-
if (point) {
43-
map.centerAndZoom(point, 16);
44-
//标注
45-
map.addOverlay(new BMapGL.Marker(point, {title: address}));
46-
lng.value = point.lng;
47-
lat.value = point.lat;
48-
lnglat.value = lng.value+','+ lat.value;
49-
} else {
50-
ElMessage.error('您选择的地址没有解析到结果!');
51-
}
52-
}, '北京市');
53-
});
54-
}
25+
nextTick(() => {
26+
const map = new BMapGL.Map('container');
27+
map.enableScrollWheelZoom(true); //滚轮缩放
28+
map.addControl(new BMapGL.CityListControl()); //添加城市列表控件
29+
map.addControl(new BMapGL.ZoomControl()); //添加缩放控件
30+
//创建地址解析器实例
31+
const geocoder = new BMapGL.Geocoder();
32+
//将地址解析结果显示在地图上,并调整地图视野
33+
geocoder.getPoint(address, (point) => {
34+
console.log('point', point);
35+
if (point) {
36+
lng.value = point.lng;
37+
lat.value = point.lat;
38+
map.centerAndZoom(point, 16);
39+
//标注
40+
map.addOverlay(new BMapGL.Marker(point, {title: address}));
41+
} else {
42+
ElMessage.error('您选择的地址没有解析到结果!');
43+
}
44+
}, '北京市');
45+
});
5546
};
5647
5748
defineExpose({initMap});

src/page/demo/baiduMap-search-locate.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const onAddress = () => {
4848
showBaiduMap.value = true;
4949
nextTick(() => {
5050
if (baiduMapRef.value) {
51-
baiduMapRef.value.initMap(null, address.value);
51+
baiduMapRef.value.initMap(address.value);
5252
}
5353
});
5454
};

0 commit comments

Comments
 (0)