gaoluyang
2026-06-24 c0cb161bb52ce0fbdce5c66ec391d107c75e2452
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<!-- 地图选择弹窗组件:基于百度地图 GL 实现 -->
<script setup lang="ts">
import { nextTick, reactive, ref } from 'vue';
 
import { useVbenModal } from '..\..\..\packages\effects\common-ui\src';
 
import { Button, Form, Input, Select, Space } from 'ant-design-vue';
 
import { loadBaiduMapSdk } from './utils';
 
const emit = defineEmits<{
  confirm: [
    data: {
      address: string;
      latitude: string;
      longitude: string;
    },
  ];
}>();
 
const mapContainerRef = ref<HTMLElement>();
const state = reactive({
  lonLat: '', // 经纬度字符串,格式为 "经度,纬度"
  address: '', // 地址信息
  loading: false, // 地址搜索加载状态
  latitude: '', // 纬度
  longitude: '', // 经度
  map: null as any, // 百度地图实例
  mapAddressOptions: [] as any[], // 地址搜索选项
  mapMarker: null as any, // 地图标记点
  geocoder: null as any, // 地理编码器实例
  mapContainerReady: false, // 地图容器是否准备好
});
 
// 初始经纬度(打开弹窗时传入)
const initLongitude = ref<number | undefined>();
const initLatitude = ref<number | undefined>();
 
/** 弹窗打开动画完成后初始化地图 */
async function handleDialogOpened() {
  // 先显示地图容器
  state.mapContainerReady = true;
 
  // 等待下一个 DOM 更新周期,确保地图容器已渲染
  await nextTick();
  // 加载百度地图 SDK
  await loadBaiduMapSdk();
  initMapInstance();
}
 
/** 弹窗关闭后清理地图 */
function handleDialogClosed() {
  // 销毁地图实例
  if (state.map) {
    state.map.destroy?.();
    state.map = null;
  }
  state.mapMarker = null;
  state.geocoder = null;
  state.mapContainerReady = false;
}
 
/** 初始化地图实例 */
function initMapInstance() {
  if (!mapContainerRef.value) {
    return;
  }
 
  // 初始化地图和地理编码器
  initMap();
  initGeocoder();
 
  // 监听地图点击事件
  state.map.addEventListener('click', (e: any) => {
    const point = e.latlng;
    state.lonLat = `${point.lng},${point.lat}`;
    regeoCode(state.lonLat);
  });
 
  // 如果有初始经纬度,加载标记点
  if (initLongitude.value && initLatitude.value) {
    const lonLat = `${initLongitude.value},${initLatitude.value}`;
    regeoCode(lonLat);
  }
}
 
/** 初始化地图 */
function initMap() {
  state.map = new window.BMapGL.Map(mapContainerRef.value);
  state.map.centerAndZoom(new window.BMapGL.Point(116.404, 39.915), 11);
  state.map.enableScrollWheelZoom();
  state.map.disableDoubleClickZoom();
 
  state.map.addControl(new window.BMapGL.NavigationControl());
  state.map.addControl(new window.BMapGL.ScaleControl());
  state.map.addControl(new window.BMapGL.ZoomControl());
}
 
/** 初始化地理编码器 */
function initGeocoder() {
  state.geocoder = new window.BMapGL.Geocoder();
}
 
/** 搜索地址 */
function autoSearch(queryValue: string) {
  if (!queryValue) {
    state.mapAddressOptions = [];
    return;
  }
 
  state.loading = true;
 
  // noinspection JSUnusedGlobalSymbols
  const localSearch = new window.BMapGL.LocalSearch(state.map, {
    onSearchComplete: (results: any) => {
      state.loading = false;
      const temp: any[] = [];
 
      if (results && results._pois) {
        results._pois.forEach((p: any) => {
          const point = p.point;
          if (point && point.lng && point.lat) {
            temp.push({
              name: p.title,
              value: `${point.lng},${point.lat}`,
            });
          }
        });
      }
 
      state.mapAddressOptions = temp;
    },
  });
 
  localSearch.search(queryValue);
}
 
/** 处理地址选择 */
function handleAddressSelect(value: unknown) {
  const selectedValue =
    typeof value === 'object' && value !== null && 'value' in value
      ? (value as { value?: number | string }).value
      : value;
  if (selectedValue !== undefined && selectedValue !== null) {
    regeoCode(String(selectedValue));
  }
}
 
/** 添加标记点 */
function setMarker(lnglat: string[]) {
  if (!lnglat) {
    return;
  }
 
  if (state.mapMarker !== null) {
    state.map.removeOverlay(state.mapMarker);
  }
 
  const point = new window.BMapGL.Point(lnglat[0], lnglat[1]);
  state.mapMarker = new window.BMapGL.Marker(point);
 
  state.map.addOverlay(state.mapMarker);
  state.map.centerAndZoom(point, 16);
}
 
/** 经纬度转地址、添加标记点 */
function regeoCode(lonLat: string) {
  if (!lonLat) {
    return;
  }
  const lnglat = lonLat.split(',');
  if (lnglat.length !== 2) {
    return;
  }
 
  state.longitude = lnglat[0]!;
  state.latitude = lnglat[1]!;
  const point = new window.BMapGL.Point(lnglat[0], lnglat[1]);
  state.map.centerAndZoom(point, 16);
 
  setMarker(lnglat);
  getAddress(lnglat);
}
 
/** 根据经纬度获取地址信息 */
function getAddress(lnglat: string[]) {
  const point = new window.BMapGL.Point(lnglat[0], lnglat[1]);
  state.geocoder.getLocation(point, (result: any) => {
    if (result && result.address) {
      state.address = result.address;
    }
  });
}
 
/** 确认选择 */
function handleConfirm() {
  if (state.longitude && state.latitude) {
    emit('confirm', {
      longitude: state.longitude,
      latitude: state.latitude,
      address: state.address,
    });
  }
  modalApi.close();
}
 
const [Modal, modalApi] = useVbenModal({
  onOpenChange(isOpen: boolean) {
    if (isOpen) {
      handleDialogOpened();
    } else {
      handleDialogClosed();
    }
  },
});
 
/** 打开弹窗 */
function open(longitude?: number, latitude?: number) {
  initLongitude.value = longitude;
  initLatitude.value = latitude;
  state.longitude = longitude ? String(longitude) : '';
  state.latitude = latitude ? String(latitude) : '';
  state.address = '';
  state.mapAddressOptions = [];
  modalApi.open();
}
 
defineExpose({ open });
</script>
 
<template>
  <Modal :footer="false" class="w-[700px]" title="百度地图">
    <div class="w-full">
      <!-- 第一行:位置搜索 -->
      <Form :label-col="{ span: 4 }">
        <Form.Item label="定位位置">
          <Select
            v-model:value="state.address"
            :filter-option="false"
            :loading="state.loading"
            :options="
              state.mapAddressOptions.map((item) => ({
                label: item.name,
                value: item.value,
              }))
            "
            allow-clear
            class="w-full"
            placeholder="可输入地址查询经纬度"
            show-search
            @search="autoSearch"
            @select="handleAddressSelect"
          />
        </Form.Item>
        <!-- 第二行:坐标显示 -->
        <Form.Item label="当前坐标">
          <Space>
            <Input
              :value="state.longitude"
              addon-before="经度"
              disabled
              style="width: 180px"
            />
            <Input
              :value="state.latitude"
              addon-before="纬度"
              disabled
              style="width: 180px"
            />
          </Space>
        </Form.Item>
      </Form>
      <!-- 第三行:地图 -->
      <div
        v-if="state.mapContainerReady"
        ref="mapContainerRef"
        class="mt-[10px] h-[400px] w-full"
      ></div>
      <div
        v-else
        class="mt-[10px] flex h-[400px] w-full items-center justify-center"
      >
        <span class="text-gray-400">地图加载中...</span>
      </div>
    </div>
    <div class="mt-4 flex justify-end gap-2">
      <Button type="primary" @click="handleConfirm">确 定</Button>
      <Button @click="modalApi.close()">取 消</Button>
    </div>
  </Modal>
</template>