Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Marker handler #79

Merged
merged 3 commits into from
May 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion ui/src/components/Map/Map.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@
import { MapContainer, TileLayer } from 'react-leaflet'
import { MapContainer, TileLayer, Marker, useMapEvent } from 'react-leaflet'
import styles from './Map.module.scss'
import PropTypes from 'prop-types'
import { flushSync } from 'react-dom'
import { createRoot } from 'react-dom/client'
import { useState } from 'react'
import { Icon } from '../Icon/Icon'
import L from 'leaflet'

const div = document.createElement('div')
const root = createRoot(div)
flushSync(() => {
A-Wodnicki marked this conversation as resolved.
Show resolved Hide resolved
root.render(<Icon name={'fullLocation'} color={'white'} size={'large'} />)
})

const LARGE_ICON_SIZE = [64, 64]
const LARGE_ICON_ANCHOR = [32, 58.675]
const LARGE_ICON_POPUP_ANCHOR = [0, -58.675]

function MarkerHandler() {
const [markers, setMarkers] = useState([])

const icon = L.divIcon({
html: div.innerHTML,
className: styles.marker,
iconSize: LARGE_ICON_SIZE,
iconAnchor: LARGE_ICON_ANCHOR,
popupAnchor: LARGE_ICON_POPUP_ANCHOR
})

useMapEvent('click', (e) => setMarkers([...markers, e.latlng]))

return markers.map((position, index) => (
<Marker key={`marker-${index}`} position={position} icon={icon} />
))
}

/**
* ### example: <br/>
Expand Down Expand Up @@ -45,6 +78,7 @@ function Map({ center, zoom }) {
style={'dark_only_labels'}
attribution={attribution}
/>
<MarkerHandler />
</MapContainer>
)
}
Expand Down