_createLocators()

in packages/layers/label/src/LabelLayer.ts [353:454]


	_createLocators() {
		const data = this.getProps('data')
		const baseAlt = this.getProps('baseAlt')
		const textWeightValues = ['none', 'normal', 'italic', 'oblique', 'bold', 'bolder']
		const markerProps: any[] = []
		for (let i = 0; i < data.length; i++) {
			const item = data[i]
			const lng = item.lng
			const lat = item.lat
			const label = item.name ?? ''
			const feature = item.feature
			const alt = baseAlt ?? 0

			const layerStyle: any = {}
			layerStyle.text_size = this.getProps('text_size') ?? 12
			layerStyle.text_family = this.getProps('text_family') ?? 'SimHei'
			layerStyle.text_weight = this.getProps('text_weight') ?? 'normal'
			layerStyle.text_background = this.getProps('text_background') ?? 'transparent'
			layerStyle.text_translate_x = this.getProps('text_translate_x') ?? 0
			layerStyle.text_translate_y = this.getProps('text_translate_y') ?? 0
			layerStyle.text_shadow_px = this.getProps('text_shadow_px') ?? 0
			const textShadowColors = this._getTextAndShadowColor(item, this.getProps('getBgColor'))
			layerStyle.text_color = textShadowColors.text
			layerStyle.text_shadow_color = textShadowColors.shadow

			// check text_weight value
			if (textWeightValues.indexOf(layerStyle.text_weight) < 0) {
				const values = textWeightValues.join('|')
				console.error(`BatchLabelLayer - Invalid text_weight value, expected values: ${values}`)
				layerStyle.text_weight = 'normal'
			}

			let finalStyle
			const styleFn = this.getProps('customTextStyle')
			if (styleFn) {
				// overwrite layerStyle with custom style
				finalStyle = {
					...layerStyle,
					...styleFn(item),
				}
			} else {
				finalStyle = layerStyle
			}

			finalStyle.textWidth = this._calcTextWidth(label, layerStyle.text_size)
			finalStyle.textHeight = layerStyle.text_size

			markerProps.push({
				lng,
				lat,
				alt,
				label,
				style: finalStyle,
				feature,
			})
		}

		// add new markers
		// this.markers.forEach(marker => {
		// 	this.remove(marker)
		// })
		// this.markers.length = 0
		// markerProps.forEach(props => {
		// 	const marker = new SimplifiedMarker(props)
		// 	this.add(marker)
		// 	this.markers.push(marker)
		// })

		// add new markers
		const length = Math.max(markerProps.length, this.markers.length)
		const newMarkers: SimplifiedMarker[] = []
		for (let i = 0; i < length; i++) {
			const props = markerProps[i]
			const marker = this.markers[i]

			// deal with markers dont need, delete
			if (!props && marker) {
				this.remove(marker)
				this.markers[i] = undefined
				continue
			}

			if (!marker) {
				const marker = new SimplifiedMarker(props)
				this.add(marker)
				newMarkers.push(marker)
			} else {
				// 复用marker,减少gc
				marker.update(props)
				newMarkers.push(marker)
			}
		}
		this.markers = newMarkers

		//
		const comparator = this.getProps('markerComparator')
		if (comparator) {
			this.markers.sort((a, b) => {
				return comparator(a ? a.feature : undefined, b ? b.feature : undefined)
			})
		}
	}