-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
132 lines (116 loc) · 3.2 KB
/
app.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
mapboxgl.accessToken = 'pk.eyJ1IjoibWluaWthcm1hIiwiYSI6IkRjTUFYdGsifQ.30RhErOKbQvLJ1kOnAl73A';
var map = new mapboxgl.Map({
container: 'map', // container id
style: 'mapbox://styles/mapbox/light-v9', // stylesheet location
center: [37.621852,55.753282], // starting position [lng, lat]
zoom: 11.5 // starting zoom
});
var modes = ["avg","median","karma","sonya","lena","artem","stepan","kolya","kristina","andrey_tyu","andrey_ba","taya","anna"],
currentMode = "median";
currentFillColor = {
"property": currentMode,
"type": "interval",
"stops": [
[0, "#32d898"],
[30,"#32aed7"],
[40,"#3275d7"],
[50,"#3244d7"],
[60,"#151875"]
]
};
menu = d3.select("#menu");
modes.forEach(m=> {
menu.append("div")
.attr("class", "menu-item")
.attr("id", m)
.text(m)
.on("click", ()=>{
setMode(m);
})
});
setMode = (mode) => {
modes.forEach(m=>{
d3.select("#"+m).attr("class", m === mode ? "menu-item-selected" : "menu-item")
});
currentMode = mode;
currentFillColor.property = mode;
map.setPaintProperty("hex", "fill-color", currentFillColor);
}
map.on("load", ()=>{
map.addSource("hex", { type: "geojson", data: "./data/hex_all.geojson"});
map.addSource("people", { type: "geojson", data: "./data/people.geojson"});
map.addLayer({
id: "hex",
source: "hex",
type: "fill",
paint: {
"fill-color": currentFillColor,
"fill-antialias": false,
"fill-opacity": 0.5
}
});
map.addLayer({
id: "people_points_stroke",
source: "people",
type: "circle",
paint: {
"circle-color": "#fff",
"circle-radius": 5,
"circle-opacity": 0.9
}
});
map.addLayer({
id: "people_points",
source: "people",
type: "circle",
paint: {
"circle-color": "#7a1ad2",
"circle-radius": 3.5
}
});
map.addLayer({
id: "people_points_names",
source: "people",
type: "symbol",
paint: {
"text-color": "#7a1ad2"
},
layout: {
"text-offset": [0.5,0],
"text-field": "{name}",
"text-justify": "left",
"text-anchor": "left",
"text-size": 11
}
});
// Create a popup, but don't add it to the map yet.
var popup = new mapboxgl.Popup({
closeButton: false,
closeOnClick: false
});
map.on('mousemove', 'hex', function(e) {
// Change the cursor style as a UI indicator.
if(e.features.length>0) {
map.getCanvas().style.cursor = 'pointer';
var coordinates = {lng: e.features[0].properties.lon, lat: e.features[0].properties.lat};
// console.log(e.lngLat);
//e.lngLat;
//[e.features[0].properties.lat,e.features[0].properties.lat];
var description = e.features[0].properties[currentMode];
// Populate the popup and set its coordinates
// based on the feature found.
popup.setLngLat(coordinates)
.setHTML(description)
.addTo(map);
} else {
map.getCanvas().style.cursor = '';
popup.remove();
}
});
map.on('mouseleave', 'hex', function() {
map.getCanvas().style.cursor = '';
popup.remove();
});
//set initial mode
setMode("median");
});