import * as turf from '@turf/turf'
|
const { VITE_APP_BASE, VITE_APP_ENV, VITE_APP_REGION_URL } = import.meta.env
|
|
|
// areaCode转换为目录结构, 返回数组,
|
export function areaCodeToArr(code) {
|
const codeStr = code.toString()
|
const provinceCode = codeStr.slice(0, 2) + '0000'
|
const cityCode = codeStr.slice(0, 4) + '00'
|
if (codeStr.slice(2, 4) === '00' && codeStr.slice(4, 6) === '00') {
|
return [provinceCode]
|
} else if (codeStr.slice(4, 6) === '00') {
|
return [provinceCode, cityCode]
|
}
|
return [provinceCode, cityCode, codeStr]
|
}
|
|
// 根据区域code获取轮廓
|
export const getContourByCode = async areaCode => {
|
const defaultDir = `${VITE_APP_REGION_URL}/100000/`
|
const hierarchy = areaCodeToArr(areaCode.slice(0, 6))
|
const jsonPathPre = hierarchy.slice(0, hierarchy.length - 1).join('/')
|
const res = await fetch(`${defaultDir}${jsonPathPre}/index.json`)
|
const parentGJson = await res.json()
|
let features = parentGJson.features.find(item => item.properties.adcode === Number(hierarchy[hierarchy.length - 1]))
|
return { type: 'FeatureCollection', features: [features] }
|
}
|