-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
148 lines (124 loc) · 3.73 KB
/
index.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
let DEFAULT_OPTIONS = {
numberOfIndicators: 1,
color: "blue",
};
let result = [];
const createDirectionIndicators = (coordinates, leafletMap, options) => {
try {
validateParameters(coordinates, leafletMap, options);
overrideDefaultOptions(options);
return createIndicator(coordinates, leafletMap);
} catch (e) {
console.error("[x] Validation failed. Error: ", e);
return [];
}
};
const validateParameters = (coordinates, leafletMap, options) => {
validateCoordinates(coordinates);
validateLeafletMap(leafletMap);
validateOptions(options);
};
const validateCoordinates = (coordinates) => {
if (coordinates == null) {
throw new Error("[x] Missing required parameter [coordinates].");
}
if (!Array.isArray(coordinates)) {
throw new Error("[x] Coordinates parameter must be an array.");
}
if (!coordinates.length) {
throw new Error("[x] Coordinates can not be an empty array.");
}
if (coordinates.length < 2) {
throw new Error("[x] Coordinates array needs at least two elements.");
}
};
const validateLeafletMap = (leafletMap) => {
if (leafletMap == null) {
throw new Error("[x] Missing required parameter [leafletMap]");
}
};
const validateOptions = (options) => {
if (options == null) {
throw new Error("[x] Missing required parameter [options]");
}
};
const overrideDefaultOptions = (options) => {
if (options.numberOfIndicators != null) {
DEFAULT_OPTIONS.numberOfIndicators = options.numberOfIndicators;
}
if (options.color != null) {
DEFAULT_OPTIONS.color = options.color;
}
};
const createIndicator = (coordinates, leafletMap) => {
for (let i = 1; i < coordinates.length; i++) {
let icon = createLeafletDivIcon(coordinates[i], coordinates[i - 1]);
for (let n = 1; n <= DEFAULT_OPTIONS.numberOfIndicators; n++) {
result.push(
window.L.marker(
getMiddlePoint(
coordinates[i],
coordinates[i - 1],
n / (DEFAULT_OPTIONS.numberOfIndicators + 1),
leafletMap
),
{
icon: icon,
}
)
);
}
}
return result;
};
const createLeafletDivIcon = (coordinates, previouseCoordinates) => {
return window.L.divIcon({
className: "direction-indicator",
bgPos: [5, 5],
html: `
<div style="color: ${
DEFAULT_OPTIONS.color
}; transform: rotate(${calculateIndicatorAngle(
previouseCoordinates,
coordinates,
-1
).toString()}deg)">▶</div>
`,
});
};
const calculateIndicatorAngle = (firstCoordinate, secondCoordinate, coef) => {
let dy = secondCoordinate[0] - firstCoordinate[0];
let dx = secondCoordinate[1] - firstCoordinate[1];
let theta = Math.cos((Math.PI / 180) * firstCoordinate[0]) * dx;
let angle = (Math.atan2(dy, theta) / Math.PI) * 180 * coef;
return angle.toFixed(2);
};
const getMiddlePoint = (
firstCoordinate,
secondCoordinate,
howMany,
leafletMap
) => {
let x = leafletMap.project(new window.L.latLng(firstCoordinate));
let y = leafletMap.project(new window.L.latLng(secondCoordinate));
let halfDistance = getDistance(x, y) * howMany;
let distance = getDistance(x, y);
if (halfDistance === 0) return leafletMap.unproject(x);
if (distance > halfDistance) {
let ratio = (distance - halfDistance) / distance;
let result = leafletMap.unproject(
new Point(y.x - ratio * (y.x - x.x), y.y - ratio * (y.y - x.y))
);
return [result.lat, result.lng];
}
};
const getDistance = (pointOne, pointTwo) => {
let x = pointTwo.x - pointOne.x;
let y = pointTwo.y - pointOne.y;
return Math.sqrt(x * x + y * y);
};
function Point(x, y, round) {
this.x = round ? Math.round(x) : x;
this.y = round ? Math.round(y) : y;
}
export { createDirectionIndicators };