right-tree/frontend/src/components/steps/location/Map.js

50 lines
1.3 KiB
JavaScript
Raw Normal View History

import { useState } from "react"
import { MapContainer, TileLayer, Marker, Popup, useMapEvents, useMap } from 'react-leaflet'
2021-10-18 17:09:56 +13:00
const NZ_BOUNDS = [
[-47.204642, 165.344238],
[-34.307144, 179.824219]
]
function LocationMarker(props) {
const [position, setPosition] = useState(null)
useMapEvents({
click(e) {
const newPosition = e.latlng;
setPosition(newPosition);
props.updateCoordinateFilter({"latitude": newPosition.lat, "longitude": newPosition.lng});
},
})
return position === null ? null : (
<Marker position={position}>
<Popup>
<strong>Latitude:</strong> {position.lat} <br/>
<strong>Longitude:</strong> {position.lng}
</Popup>
</Marker>
)
}
2021-10-18 17:09:56 +13:00
function FitNewZealandBounds() {
const map = useMap()
map.fitBounds(NZ_BOUNDS)
return null
}
export default function Map(props) {
return (
<div className="map-container">
2021-10-18 17:09:56 +13:00
<MapContainer scrollWheelZoom={true}>
<TileLayer
attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<LocationMarker updateCoordinateFilter={props.updateFilterState} />
2021-10-18 17:09:56 +13:00
<FitNewZealandBounds />
</MapContainer>
</div>
)
}