-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
app.tsx
92 lines (82 loc) · 2.24 KB
/
app.tsx
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
import * as React from 'react';
import {useState, useMemo} from 'react';
import {createRoot} from 'react-dom/client';
import Map, {
Marker,
Popup,
NavigationControl,
FullscreenControl,
ScaleControl,
GeolocateControl
} from 'react-map-gl';
import ControlPanel from './control-panel';
import Pin from './pin';
import CITIES from '../../.data/cities.json';
const TOKEN = ''; // Set your mapbox token here
export default function App() {
const [popupInfo, setPopupInfo] = useState(null);
const pins = useMemo(
() =>
CITIES.map((city, index) => (
<Marker
key={`marker-${index}`}
longitude={city.longitude}
latitude={city.latitude}
anchor="bottom"
onClick={e => {
// If we let the click event propagates to the map, it will immediately close the popup
// with `closeOnClick: true`
e.originalEvent.stopPropagation();
setPopupInfo(city);
}}
>
<Pin />
</Marker>
)),
[]
);
return (
<>
<Map
initialViewState={{
latitude: 40,
longitude: -100,
zoom: 3.5,
bearing: 0,
pitch: 0
}}
mapStyle="mapbox://styles/mapbox/dark-v9"
mapboxAccessToken={TOKEN}
>
<GeolocateControl position="top-left" />
<FullscreenControl position="top-left" />
<NavigationControl position="top-left" />
<ScaleControl />
{pins}
{popupInfo && (
<Popup
anchor="top"
longitude={Number(popupInfo.longitude)}
latitude={Number(popupInfo.latitude)}
onClose={() => setPopupInfo(null)}
>
<div>
{popupInfo.city}, {popupInfo.state} |{' '}
<a
target="_new"
href={`http://en.wikipedia.org/w/index.php?title=Special:Search&search=${popupInfo.city}, ${popupInfo.state}`}
>
Wikipedia
</a>
</div>
<img width="100%" src={popupInfo.image} />
</Popup>
)}
</Map>
<ControlPanel />
</>
);
}
export function renderToDom(container) {
createRoot(container).render(<App />);
}