forked from shahibuzzaman/covid19-tracker-reactJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapContainer.js
More file actions
159 lines (139 loc) · 4.33 KB
/
MapContainer.js
File metadata and controls
159 lines (139 loc) · 4.33 KB
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
import React, { useEffect, useRef } from 'react';
import mapboxgl from 'mapbox-gl';
import useSWR from 'swr';
import 'mapbox-gl/dist/mapbox-gl.css';
import './Map.css';
mapboxgl.accessToken =
'pk.eyJ1Ijoic2hhaGlidXp6YW1hbiIsImEiOiJjazhtMjlsZGMwZTdwM2xvNHYyZWZnaW95In0.wv7TDBBzK5g_Rqwi32PWag';
function MapContainer() {
const mapContainer = useRef(null);
const fetcher = (url) =>
fetch(url)
.then((r) => r.json())
.then((data) =>
data.map((point, index) => ({
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [point.countryInfo.long, point.countryInfo.lat],
},
properties: {
id: index,
country: point.country,
cases: point.cases,
recovered: point.recovered,
deaths: point.deaths,
todayDeaths: point.todayDeaths,
todayCases: point.todayCases,
flag: point.countryInfo.flag,
},
}))
);
const { data } = useSWR('https://corona.lmao.ninja/v2/countries', fetcher);
useEffect(() => {
if (data) {
const map = new mapboxgl.Map({
container: mapContainer.current,
style: 'mapbox://styles/shahibuzzaman/ck909hv6e00o81ik4qee1l32u',
center: [16, 27],
zoom: 1,
});
map.once('load', function () {
map.addSource('points', {
type: 'geojson',
data: {
type: 'FeatureCollection',
features: data,
},
});
map.addLayer({
id: 'circles',
source: 'points',
type: 'circle',
paint: {
'circle-opacity': 0.75,
'circle-stroke-width': 1,
'circle-radius': [
'interpolate',
['linear'],
['get', 'cases'],
1,
4,
1000,
8,
4000,
10,
8000,
14,
12000,
18,
100000,
24,
500000,
30,
1000000,
40,
],
'circle-color': 'red',
},
});
const popup = new mapboxgl.Popup({
closeButton: false,
closeOnClick: false,
});
let lastId;
map.on('mousemove', 'circles', (e) => {
const id = e.features[0].properties.id;
if (id !== lastId) {
lastId = id;
map.getCanvas().style.cursor = 'pointer';
const {
cases,
deaths,
country,
recovered,
flag,
todayCases,
todayDeaths,
} = e.features[0].properties;
const coordinates = e.features[0].geometry.coordinates.slice();
const mortalityRate = ((deaths / cases) * 100).toFixed(2);
const recoverRate = ((recovered / cases) * 100).toFixed(2);
const countryFlag = `<img src=${flag}></img>`;
const HTML = `${countryFlag} <h4>${country}</h4>
<div class="middle">
<hr>
<p>Cases: <b>${cases}</b></p>
<p style="color:red;">Deaths: <b>${deaths}</b></p>
<p style="color:green;">Recovered : <b>${recovered}</b></p>
<p>Mortality Rate: <b>${mortalityRate}%</b></p>
<p>Recover Rate: <b>${recoverRate}%</b></p>
<hr>
</div>
<div class="middle">
<p>New Cases : <b>${todayCases}</b></p>
<p>New Death : <b>${todayDeaths}</b></p>
</div>
`;
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
}
popup.setLngLat(coordinates).setHTML(HTML).addTo(map);
}
});
map.on('mouseleave', 'circles', function () {
lastId = undefined;
map.getCanvas().style.cursor = '';
popup.remove();
});
});
}
}, [data]);
return (
<div>
<div className=''></div>
<div ref={mapContainer} className='mapContainer shadow-sm' />
</div>
);
}
export default MapContainer;