forked from kamicut/lebanon-protests-2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.js
220 lines (190 loc) · 5.89 KB
/
map.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
mapboxgl.accessToken = 'pk.eyJ1Ijoia2FtaWN1dCIsImEiOiJMVzF2NThZIn0.WO0ArcIIzYVioen3HpfugQ';
mapboxgl.setRTLTextPlugin('https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-rtl-text/v0.2.3/mapbox-gl-rtl-text.js');
var map = new mapboxgl.Map({
container: 'map', // container id
style: 'mapbox://styles/kamicut/ck1vedq510l101cocnbgs1ira',
center: [0, 0], // starting position [lng, lat]
zoom: 1 // starting zoom
});
function addProtestsLayer(map) {
map.addLayer({
id: 'geo_incidents',
type: "symbol",
source: {
"type": "geojson",
"data": "protests.geojson"
},
layout: {
"text-field": ["to-string", ["get", "protest location"]],
"icon-image": ["case",
["has", "tweet_id"], "marker-15",
["has", "instagram_url"], "marker-15",
"none"
],
"text-offset": [-1, 1],
"icon-size": 1,
"text-size": 15,
"text-allow-overlap": false,
"icon-allow-overlap": true
},
"paint": {
"text-opacity": ["case",
["has", "tweet_id"], 1,
["has", "instagram_url"], 1,
0.4
],
"text-color": "hsl(0, 50%, 90%)",
"text-halo-color": "hsl(0, 100%, 0%)",
"text-halo-width": 1
}
}, )
}
function addProtestsHeatmap(map) {
map.addLayer({
id: 'sat_incidents',
type: "heatmap",
source: {
"type": "geojson",
"data": "protests.geojson"
},
"paint": {
"heatmap-radius": [
"interpolate",
["linear"],
["zoom"],
0, 5,
9, 20
],
"heatmap-color": [
"interpolate",
["linear"],
["heatmap-density"],
0,
"hsla(27, 100%, 50%, 0)",
0.5,
"hsl(35, 100%, 50%)",
1,
"red"
],
"heatmap-opacity": 0.6
},
}, )
}
function forEachLayer(map, text, cb) {
map.getStyle().layers.forEach((layer) => {
if (!layer.id.includes(text)) return;
cb(layer);
});
}
// Changing the map base style
function changeStyle(map, style) {
const savedLayers = [];
const savedSources = {};
const layerGroups = [
'sat_incidents',
'geo_incidents',
];
layerGroups.forEach((layerGroup) => {
forEachLayer(map, layerGroup, (layer) => {
savedSources[layer.source] = map.getSource(layer.source).serialize();
savedLayers.push(layer);
});
});
map.setStyle(`mapbox://styles/${style}`);
setTimeout(() => {
Object.entries(savedSources).forEach(([id, source]) => {
map.addSource(id, source);
});
savedLayers.forEach((layer) => {
map.addLayer(layer);
});
}, 1000);
}
map.on('load', function () {
addProtestsHeatmap(map)
addProtestsLayer(map)
})
var layerList = document.getElementById('menu');
var inputs = layerList.getElementsByTagName('input');
function switchLayer(layer) {
var layerId = layer.target.id;
changeStyle(map, layerId)
}
for (var i = 0; i < inputs.length; i++) {
inputs[i].onclick = switchLayer;
}
// hide button
var hideButton = document.querySelector('#hide')
var menubox = document.querySelector('#menubox')
var loader = document.querySelector('#loader')
var menuBoxShow = true
function toggleMenuBox() {
menuBoxShow = !menuBoxShow
menubox.style = menuBoxShow ? "" : "display: none"
hideButton.innerHTML = menuBoxShow ? "[hide]" : "[show]"
}
hideButton.onclick = function (e) {
toggleMenuBox()
}
// add popup
// Create a popup, but don't add it to the map yet.
var popup = new mapboxgl.Popup({
closeButton: false,
closeOnClick: false
});
map.on('click', 'geo_incidents', async function (e) {
menubox.innerHTML = ''
var coordinates = e.features[0].geometry.coordinates.slice();
var properties = e.features[0].properties
var link = properties.links
var description = 'غير موثق';
if (properties.tweet_id) {
loader.style = "display: block;"
twttr.widgets.createTweet(properties.tweet_id, menubox)
.then(() => {
loader.style = "display: none;"
if (!menuBoxShow) {
toggleMenuBox()
}
})
} else if (properties.instagram_url) {
loader.style = "display: block;"
fetch(`https://noembed.com/embed?url=${properties.instagram_url}`)
.then(data => data.json())
.then(data => {
loader.style = "display: none;"
menubox.innerHTML = data.html
if (!menuBoxShow) {
toggleMenuBox()
}
})
} else {
if (link !== "null") {
description = `<a target=_blank href=${link}>رابط</a>`
}
if (properties.category && properties.category !== "null") {
description = description + "<br>" + properties.category
}
if (properties.last_updated && properties.last_updated !== "null") {
description = description + "<br>" + properties.last_updated
}
// Ensure that if the map is zoomed out such that multiple
// copies of the feature are visible, the popup appears
// over the copy being pointed to.
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
}
// Populate the popup and set its coordinates
// based on the feature found.
popup.setLngLat(coordinates)
.setHTML(description)
.addTo(map);
}
})
map.on('mouseenter', 'geo_incidents', function (e) {
// Change the cursor style as a UI indicator.
map.getCanvas().style.cursor = 'pointer';
});
map.on('mouseleave', 'geo_incidents', function () {
map.getCanvas().style.cursor = '';
});