Last active
March 4, 2023 21:30
-
-
Save JenniferFuBook/0c39c8b100860c8863b289778508dc92 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useRef, useState } from 'react'; | |
import { MapContainer, TileLayer, Marker, Popup } from 'react-leaflet'; | |
import { Icon } from 'leaflet'; | |
import 'leaflet/dist/leaflet.css'; | |
import markerIconPng from 'leaflet/dist/images/marker-icon.png'; | |
import markShadowPng from 'leaflet/dist/images/marker-shadow.png'; | |
function App() { | |
const [popupRefReady, setPopupRefReady] = useState(false); | |
const markerRef = useRef(); | |
useEffect(() => { | |
markerRef.current?.openPopup(); | |
}, [popupRefReady]); | |
return ( | |
<MapContainer center={[38.907192, -77.036873]} zoom={8}> | |
<TileLayer | |
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors' | |
url="https://tile.openstreetmap.org/{z}/{x}/{y}{r}.png" | |
/> | |
<Marker | |
position={[38.907192, -77.036873]} | |
icon={ | |
new Icon({ | |
iconUrl: markerIconPng, | |
shadowUrl: markShadowPng, | |
}) | |
} | |
ref={markerRef} | |
> | |
<Popup ref={() => setPopupRefReady(true)}> | |
Washington, D.C. is the capital city and federal district of the United States. | |
</Popup> | |
</Marker> | |
</MapContainer> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment