-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathcommon.ts
88 lines (81 loc) · 2.22 KB
/
common.ts
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
import { Scene } from '@antv/l7';
import { Position } from '@turf/turf';
import { ILayerMouseEvent, ILngLat, ISceneMouseEvent } from '../typings';
import { isEqual } from 'lodash';
// @ts-ignore
export const isDev = process.env.NODE_ENV === 'development';
/**
* 获取完全覆盖地图区域的DOM,会根据地图类型返回不同的结果
* @param scene
*/
export const getMapDom = (scene: Scene): HTMLElement | null => {
const container = scene.getContainer();
return (
container?.querySelector('.l7-marker-container') ??
container?.querySelector('.BMap_mask') ??
container?.querySelector('#tdt-L7') ??
scene.getMapCanvasContainer() ??
container?.querySelector('.l7-scene') ??
container?.querySelector('.l7-control-container') ??
container?.querySelector('.l7-marker-container2') ??
null
);
};
/**
* 磨平L7 Scene 鼠标事件返回的经纬度差异
* @param e
*/
export const getLngLat = (e: ISceneMouseEvent | ILayerMouseEvent) => {
// @ts-ignore
return e.lngLat || e.lnglat || e.latlng;
};
export const getPosition: (
e: ISceneMouseEvent | ILayerMouseEvent,
) => Position = (e) => {
const { lng, lat } = getLngLat(e);
return [lng, lat];
};
/**
* 将lnglat转换为position格式
* @param lng
* @param lat
*/
export const transLngLat2Position: (lngLat: ILngLat) => Position = ({
lng,
lat,
}) => [lng, lat];
/**
* 找到最小值的下标
* @param array
*/
export const findMinIndex = (array: number[]) => {
let maxValue = Number.MAX_SAFE_INTEGER;
let maxIndex = 0;
const length = array.length;
for (let index = 0; index < length; index++) {
if (array[index] < maxValue) {
maxValue = array[index];
maxIndex = index;
}
}
return maxIndex;
};
export const splitByPosition = (
positions: Position[],
splitPosition: Position,
) => {
const linePositionsList: Position[][] = [];
let linePositions: Position[] = [];
positions.forEach((position) => {
if (!isEqual(position, splitPosition)) {
linePositions.push(position);
} else if (linePositions.length) {
linePositionsList.push(linePositions);
linePositions = [];
}
});
if (linePositions.length) {
linePositionsList.push(linePositions);
}
return linePositionsList;
};