-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.js
80 lines (69 loc) · 2.3 KB
/
main.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
import mapStyle from './style.js'
import colors from './species_colors.js'
const $map = document.getElementById('map');
const $controls = document.getElementById('controls');
const $loader = document.getElementById('loader');
const EUROPE_CENTER = {lat: 47.582798, lng: 9.707756};
const {ScatterplotLayer, GoogleMapsOverlay} = deck;
let GMAP , DECKGL_OVERLAY, DATA_COUNTRY;
async function updateMap() {
const $activeElement = document.querySelector(".is-active");
if ($activeElement) {
$activeElement.classList.remove('is-active')
}
$loader.classList.add("is-active");
this.classList.add('is-active')
let country = this.textContent.replace(/ /g,'').toLowerCase();
let layer = await getLayer(country);
DECKGL_OVERLAY.setProps({layers: [layer]});
if(country === 'all'){
GMAP.setCenter(EUROPE_CENTER);
GMAP.setZoom(4);
}else{
GMAP.setCenter({lat: DATA_COUNTRY[0].lat,lng: DATA_COUNTRY[0].lng })
GMAP.setZoom(5);
}
$loader.classList.remove("is-active");
}
async function getLayer(country = 'austria'){
const request_country = await fetch(`./data/${country}.json`);
DATA_COUNTRY = await request_country.json();
return await new ScatterplotLayer({
id : 'trees',
data: DATA_COUNTRY,
opacity: 0.8,
stroked: false,
filled: true,
radiusScale: 6,
radiusMinPixels: 1,
radiusMaxPixels: 100,
lineWidthMinPixels: 1,
getPosition: d=> [d.lng, d.lat],
getRadius : d => 50,
getFillColor: d=> colors[d.specie],
//getLineColor: d=> [30,30,30]
});
}
function renderButtonElement(country) {
const element = document.createElement('button')
element.textContent = country
element.addEventListener('click', updateMap)
$controls.appendChild(element)
}
async function renderButtons() {
const response = await fetch(`./catalogs/countries.json`)
const countries = await response.json()
countries.forEach(renderButtonElement);
}
async function initMap(){
GMAP = new google.maps.Map($map, {
zoom: 4,
center: EUROPE_CENTER,
styles: mapStyle,
});
DECKGL_OVERLAY = new GoogleMapsOverlay();
DECKGL_OVERLAY.setMap(GMAP);
DECKGL_OVERLAY.setProps({ layers: [ await getLayer()] })
renderButtons()
}
google.maps.event.addDomListener(window,'load', initMap);