-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.html
177 lines (156 loc) · 4.58 KB
/
index.html
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hub and spoke</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<link href="https://api.mapbox.com/mapbox-gl-js/v2.9.0-beta.2/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v2.9.0-beta.2/mapbox-gl.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@turf/turf@6/turf.min.js"></script>
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<div id="map"></div>
<script>
let initLoad = true;
mapboxgl.accessToken = 'pk.eyJ1Ijoid2lsbGlhbS1kYXZpcyIsImEiOiJja2tiYWtvbWcwMmtzMnBvajh3M2kxZGRuIn0.GeROaHKn2Gbvsg1e8buJEw';
const map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11',
center: [0, 40],
zoom: 3,
projection: 'globe',
maxZoom: 6
});
map.on('load', () => {
let airports;
map.once('idle', () => {
d3.json("./data/airport.json", function (d) {
airports = d;
getSpoke(airports);
});
// use mouse location
// map.on('mousemove', (e) => {
// const newPoint = turf.point([e.lngLat.lng, e.lngLat.lat]);
// buildSpoke(airports, newPoint);
// });
// use map center
map.on('move', () => {
getSpoke(airports);
});
});
});
function getSpoke(airports) {
const center = map.getCenter();
const newPoint = turf.point([center.lng, center.lat]);
buildSpoke(airports, newPoint);
}
function buildSpoke(airports, point) {
let nearestAirports = turf.featureCollection([]);
let nearestAirportLines = turf.featureCollection([]);
let cleanedAirports = JSON.parse(JSON.stringify(airports));
for (let i=1;i<=10;i++) {
const nearest = turf.nearestPoint(point, cleanedAirports);
const startLng = point.geometry.coordinates[0];
const endLng = nearest.geometry.coordinates[0];
if (startLng >= 90 && endLng <= -90) {
nearest.geometry.coordinates[0] += 360;
} else if (startLng <= -90 && endLng >= 90) {
nearest.geometry.coordinates[0] -= 360;
}
const nearestLine = turf.lineString([point.geometry.coordinates, nearest.geometry.coordinates]);
nearestAirports.features.push(nearest);
nearestAirportLines.features.push(nearestLine);
const index = cleanedAirports.features.findIndex(n => n.properties.name === nearest.properties.name)
if (index !== -1) {
cleanedAirports.features.splice(index, 1);
}
}
if (initLoad) {
addLayers(airports, nearestAirports, nearestAirportLines);
} else {
map.getSource('newPoint').setData(nearestAirports);
map.getSource('newLine').setData(nearestAirportLines);
}
}
function addLayers(airports, nearest, route) {
initLoad = false;
map.addSource('points', {
'type': 'geojson',
'data': airports
});
map.addSource('newPoint', {
'type': 'geojson',
'data': nearest
});
map.addSource('newLine', {
'type': 'geojson',
'data': route
});
map.addLayer({
'id': 'routeLayer',
'type': 'line',
'source': 'newLine',
'layout': {
'line-join': 'round',
'line-cap': 'round'
},
'paint': {
'line-color': 'red',
'line-width': [
'interpolate',
['linear'],
['zoom'],
0,
0.5,
3,
4
]
}
});
map.addLayer({
'id': 'globe-points',
'type': 'circle',
'source': 'points',
'paint': {
'circle-radius': [
'interpolate',
['linear'],
['zoom'],
0,
0.1,
3,
3
],
'circle-opacity': 1,
'circle-blur': 0,
'circle-color': '#555'
}
});
map.addLayer({
'id': 'globe-newPoint',
'type': 'circle',
'source': 'newPoint',
'paint': {
'circle-radius': [
'interpolate',
['linear'],
['zoom'],
0,
0.25,
3,
4
],
'circle-opacity': 1,
'circle-blur': 0,
'circle-color': 'red'
}
});
}
</script>
</body>
</html>