This repository has been archived by the owner on Apr 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
146 lines (129 loc) · 3.98 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
/* ! Most parts taken from https://github.com/uber/deck.gl/pull/2179 ! */
import loadScript from './src/load-gmaps';
import initMap from './src/init-map';
import initDeck from './src/init-deck';
import initOverlay from './src/init-overlay';
import TripsLayer from '@deck.gl/experimental-layers/src/trips-layer/index';
import {GeoJsonLayer, IconLayer} from '@deck.gl/layers';
import {getTrips} from './src/flight-trips.js';
import {randomPoint} from '@turf/random';
import styles from './basemap.json'; // https://snazzymaps.com/style/1261/dark
const gmUrl = `https://maps.googleapis.com/maps/api/js?key=${process.env.GOOGLE_MAPS_API_KEY}&libraries=visualization&v=3.34`
const TILE_SIZE = 256;
const INITIAL_VIEW_STATE = {
latitude: 51.46,
longitude: -0.4,
zoom: 8,
pitch: 0
};
const mapOptions = {
styles,
center: {
lat: INITIAL_VIEW_STATE.latitude,
lng: INITIAL_VIEW_STATE.longitude
},
// Adding 1 to the zoom level get us close to each other
zoom: INITIAL_VIEW_STATE.zoom + 1,
tilt: INITIAL_VIEW_STATE.pitch
};
let frame = 0
let map = null;
let deck = null;
let hoveredFeature = null;
const trips = getTrips();
const randomPointsDeckGl = randomPoint(50, {bbox: [-2, 50, 2, 53]});
const randomPointsGoogle = randomPoint(50, {bbox: [-2, 50, 2, 53]});
(async () => {
await loadScript(gmUrl);
const map = initMap('map', mapOptions);
const deck = initDeck(map, INITIAL_VIEW_STATE);
const overlay = initOverlay(map, deck, TILE_SIZE);
addGoogleMarkers(map, randomPointsGoogle);
addDeckListeners(deck);
function animate() {
frame = (frame + 0.1) % 400;
deck.setProps({layers: getLayers(frame)})
requestAnimationFrame(animate);
}
animate();
})();
function addDeckListeners(deck) {
window.addEventListener('click', event => {
const {clientX: x, clientY: y} = event;
const picked = deck.pickObject({x, y, radius: 4, layerIds: ['icon-layer']});
if (picked) {
alert('Clicked on a Deck.gl object');
}
});
window.addEventListener('mousemove', event => {
if (!deck.layerManager) {
return;
}
const {clientX: x, clientY: y} = event;
const picked = deck.pickObject({x, y, radius: 0, layerIds: ['icon-layer']});
if (picked && hoveredFeature !== picked.object) {
hoveredFeature = picked.object;
deck.setProps({layers: getLayers(frame)});
document.body.classList.add('cursor-pointer');
} else if(!picked) {
hoveredFeature = null;
deck.setProps({layers: getLayers(frame)});
document.body.classList.remove('cursor-pointer');
}
});
}
function addGoogleMarkers(map, points) {
points.features.forEach(point => {
const marker = new google.maps.Marker({
position: {
lng: point.geometry.coordinates[0],
lat: point.geometry.coordinates[1]
},
map: map,
});
marker.addListener('click', () => {
alert('Clicked on a Google Maps object');
});
})
}
function getLayers(frame) {
const ICON_MAPPING = {
marker: {x: 0, y: 0, width: 128, height: 128, anchorY: 128, anchorX: 64, mask: true}
};
return [
new IconLayer({
id: 'icon-layer',
data: randomPointsDeckGl.features,
pickable: true,
iconAtlas: 'http://deck.gl/images/icon-atlas.png',
iconMapping: ICON_MAPPING,
getIcon: d => 'marker',
sizeScale: 10,
getPosition: d => d.geometry.coordinates,
getSize: d => 5,
getColor: d => [255, 0, 0]
}),
new IconLayer({
id: 'icon-layer-hovered',
data: hoveredFeature ? [hoveredFeature] : null,
pickable: true,
iconAtlas: 'http://deck.gl/images/icon-atlas.png',
iconMapping: ICON_MAPPING,
getIcon: d => 'marker',
sizeScale: 10,
getPosition: d => d.geometry.coordinates,
getSize: d => 5,
getColor: d => [255, 255, 255]
}),
new TripsLayer({
id: 'trips',
data: trips,
getPath: d => d.path,
getColor: d => d.color,
opacity: 0.8,
strokeWidth: 1,
trailLength: 10,
currentTime: frame
})
];
}