吉安感知网项目-前端
shuishen
2026-02-03 89380e6260a75d1d3b94de687ebcc2f50d50659d
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
import { areaCodeToArr, getContourByCode } from '@/utils/cesium/mapUtil'
import { useStore } from 'vuex'
 
const { VITE_APP_REGION_URL } = import.meta.env
const defaultDir = `${VITE_APP_REGION_URL}/100000/`
// 获取文件路径
const getFiler = async url => {
    const res = await fetch(url)
    return await res.json()
}
 
export const useAreaBoundary = options => {
    const {
        initFly = false, //初始飞行到轮廓
    } = options || {}
    let map = null //地图实例
    // 地图层级
    const scalingJudgment = [
        { name: '县', zoomRange: [9.1, 20] },
        { name: '市', zoomRange: [5.1, 9.1] },
        { name: '省', zoomRange: [0, 5.1] },
    ]
    const store = useStore()
    const userAreaCode = computed(() => store.state.user.userInfo.detail.areaCode)
    const areaCode = userAreaCode.value
    const hierarchy = areaCodeToArr(areaCode.slice(0, 6))
    const jsonPath = hierarchy.join('/')
    let active = null //当前放大所属层级
    let geoJsonLayer = null
    let maxGeoJson = null //最大边界
 
    // 初始化各个层级对应geojson
    async function initLevelGJson() {
        // 省级账号
        if (hierarchy.length === 1) {
            maxGeoJson = await getFiler(`${defaultDir}${jsonPath}/indexDistrict.json`) //区县边界
            const gJson2 = await getFiler(`${defaultDir}${jsonPath}/index.json`) //市边界
            scalingJudgment[0].gJson = maxGeoJson
            scalingJudgment[1].gJson = gJson2
            scalingJudgment[2].gJson = gJson2
        }
        // 市
        if (hierarchy.length === 2) {
            maxGeoJson = await getFiler(`${defaultDir}${jsonPath}/index.json`)
            scalingJudgment[0].gJson = maxGeoJson
            scalingJudgment[1].gJson = maxGeoJson
            scalingJudgment[2].gJson = maxGeoJson
        }
        if (hierarchy.length === 3) {
            maxGeoJson = await getContourByCode(areaCode.slice(0, 6))
            scalingJudgment[0].gJson = maxGeoJson
            scalingJudgment[1].gJson = maxGeoJson
            scalingJudgment[2].gJson = maxGeoJson
        }
    }
 
    // flyMaxBoundary
    function flyMaxBoundary() {
        let bj = L.geoJSON(maxGeoJson, {
            style: feature => ({
                color: 'rgba(255,120,0,0)',
                fillOpacity: 0,
            }),
        }).addTo(map)
        map.fitBounds(bj.getBounds())
        map.removeLayer(bj)
    }
 
    // 渲染边界轮廓
    function renderOutline(row) {
        geoJsonLayer && map.removeLayer(geoJsonLayer)
        geoJsonLayer = L.geoJSON(row.gJson, {
            style: feature => ({
                color: '#00F9EC', // 边界线颜色
                weight: 2, // 边界线宽度
                fillOpacity: 0, // 填充透明度
            }),
        }).addTo(map)
    }
 
    // 缩放回调
    function zoomendChange() {
        let zoom = map.getZoom()
        for (let [index, item] of scalingJudgment.entries()) {
            if (zoom > item.zoomRange[0] && zoom <= item.zoomRange[1]) {
                if (active === item.name) return
                active = item.name
                renderOutline(item)
                break
            }
        }
    }
 
    // 初始化边界
    async function initBoundary(mapObj) {
        map = mapObj
        await initLevelGJson()
        initFly && flyMaxBoundary()
        zoomendChange()
        map.on('zoomend', zoomendChange)
    }
 
    return {
        initBoundary,
        flyMaxBoundary,
    }
}