| | |
| | | } |
| | | }, { deep: true }) |
| | | |
| | | // 获取行政区划信息 |
| | | async function getAdministrativeInfo(lng: number, lat: number) { |
| | | return new Promise<{ |
| | | provinceName: string |
| | | provinceCode: string |
| | | cityName: string |
| | | cityCode: string |
| | | districtName: string |
| | | districtCode: string |
| | | } | null>((resolve) => { |
| | | if (typeof AMap === 'undefined') { |
| | | resolve(null) |
| | | return |
| | | } |
| | | const geocoder = new AMap.Geocoder({ |
| | | city: '全国', |
| | | }) |
| | | geocoder.getAddress([lng, lat], (status: string, result: any) => { |
| | | if (status === 'complete' && result.regeocode) { |
| | | const comp = result.regeocode.addressComponent |
| | | const adcode = String(comp.adcode) // 6位区县编码 |
| | | |
| | | // 提取省市区代码 |
| | | const provinceCode = `${adcode.substring(0, 2)}0000` |
| | | const cityCode = `${adcode.substring(0, 4)}00` |
| | | const districtCode = adcode |
| | | |
| | | resolve({ |
| | | provinceName: comp.province, |
| | | provinceCode, |
| | | cityName: Array.isArray(comp.city) ? comp.province : (comp.city || comp.province), // 直辖市 city 可能为空或数组 |
| | | cityCode: comp.citycode || cityCode, // 优先使用 AMap 的 citycode |
| | | districtName: comp.district, |
| | | districtCode, |
| | | }) |
| | | } |
| | | else { |
| | | resolve(null) |
| | | } |
| | | }) |
| | | }) |
| | | } |
| | | |
| | | // 保存点位 |
| | | async function handleSavePoint() { |
| | | if (!currentPoint.value) { |
| | |
| | | } |
| | | } |
| | | |
| | | // 6. 获取省市区信息 |
| | | let administrativeInfo = { |
| | | provinceName: '', |
| | | provinceCode: '', |
| | | cityName: '', |
| | | cityCode: '', |
| | | districtName: '', |
| | | districtCode: '', |
| | | } |
| | | if (currentPoint.value.lon && currentPoint.value.lat) { |
| | | const info = await getAdministrativeInfo(Number(currentPoint.value.lon), Number(currentPoint.value.lat)) |
| | | if (info) { |
| | | administrativeInfo = info |
| | | } |
| | | } |
| | | |
| | | // 兜底:如果没有匹配到任何区域,或者计算失败,尝试使用默认值(虽然业务上应该报错提示) |
| | | if (!electronicAreaId) { |
| | | // const fallbackArea = electronicFenceAreas.value.find(area => area.areaType === 1) || electronicFenceAreas.value[0] |
| | |
| | | } |
| | | |
| | | const payload = { |
| | | ...administrativeInfo, |
| | | pointName: currentPoint.value.name, |
| | | pointAddress: currentPoint.value.address, |
| | | pointLon: currentPoint.value.lon, |