mirror of
https://github.com/visgl/react-map-gl.git
synced 2026-01-18 15:54:22 +00:00
88 lines
2.0 KiB
TypeScript
88 lines
2.0 KiB
TypeScript
import * as React from 'react';
|
|
import {useState, useMemo} from 'react';
|
|
import {render} from 'react-dom';
|
|
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"
|
|
>
|
|
<Pin onClick={() => setPopupInfo(city)} />
|
|
</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)}
|
|
closeOnClick={false}
|
|
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) {
|
|
render(<App />, container);
|
|
}
|