in examples-vite/layers/label/base.ts [72:124]
async function getLabelData() {
const urls = await fetch('https://fbi-geography.oss-cn-hangzhou.aliyuncs.com/urls.json')
const { base_url, uris, adcode_placeholder } = await urls.json()
const res = await fetch(base_url + uris.all_100000_json)
const geoJsonData = await res.json()
const countryRegion = { ...geoJsonData, features: [] }
const regionsRegion = { ...geoJsonData, features: [] }
const provinceRegion = { ...geoJsonData, features: [] }
const cityRegion = { ...geoJsonData, features: [] }
const districtRegion = { ...geoJsonData, features: [] }
geoJsonData.features.forEach((feature) => {
const level = feature.properties.level
const adcode = feature.properties.adcode
if (adcode === '100000') {
countryRegion.features.push(feature)
return
}
if (new Set(['region', 'province', 'city', 'district']).has(level)) {
if (level === 'region') {
regionsRegion.features.push(feature)
} else if (level === 'province') {
provinceRegion.features.push(feature)
} else if (level === 'city') {
cityRegion.features.push(feature)
} else if (level === 'district') {
districtRegion.features.push(feature)
}
}
})
const labels: any[] = []
for (let i = 0; i < provinceRegion.features.length; i++) {
const feature = provinceRegion.features[i]
const properties = feature.properties
if (!properties) continue
let center
if (properties.centroid) {
center = properties.centroid
} else if (Array.isArray(properties.center)) {
center = properties.center
} else if (typeof properties.center === 'string') {
center = properties.center.split(',').map((v) => parseFloat(v))
}
labels.push({
lng: center[0],
lat: center[1],
name: `${properties?.name} ${Math.random().toFixed(5)}` ?? '',
feature: feature,
})
}
return labels
}