-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3fbe0b5
commit 36de7ee
Showing
10 changed files
with
1,114 additions
and
12,239 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
105 changes: 76 additions & 29 deletions
105
src/components/Map/components/LocationControl/LocationControl.jsx
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,87 @@ | ||
import Leaflet from 'leaflet'; | ||
import React, { useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Marker, CircleMarker, useMap } from 'react-leaflet'; | ||
import { Button, Box } from '@mui/material'; | ||
import MyLocationIcon from '@mui/icons-material/MyLocation'; | ||
import Control from 'react-leaflet-custom-control'; | ||
import ReactDOMServer from 'react-dom/server'; | ||
import L from 'leaflet'; | ||
|
||
const LocationIcon = () => { | ||
const icon = document.createElement('img'); | ||
icon.style.width = '22px'; | ||
icon.style.height = '22px'; | ||
icon.src = | ||
'https://cdn3.iconfinder.com/data/icons/google-material-design-icons/48/ic_my_location_48px-512.png'; | ||
const LocationButton = ({ userPosition }) => { | ||
const map = useMap(); | ||
|
||
return icon; | ||
const handleFlyToLocationClick = () => { | ||
map.flyTo(userPosition, map.getZoom()); | ||
}; | ||
|
||
return ( | ||
<Control prepend position="bottomright"> | ||
<Button onClick={handleFlyToLocationClick}> | ||
<Box | ||
sx={{ | ||
boxShadow: 1.3, | ||
border: 0.1, | ||
color: 'black', | ||
padding: 0.5, | ||
background: 'white', | ||
}} | ||
> | ||
<MyLocationIcon sx={{ color: 'black', fontSize: 22 }} /> | ||
</Box> | ||
</Button> | ||
</Control> | ||
); | ||
}; | ||
|
||
Leaflet.Control.Button = Leaflet.Control.extend({ | ||
options: { | ||
position: 'bottomright', | ||
}, | ||
const createLocationIcon = () => { | ||
const locationIconJSX = <MyLocationIcon sx={{ color: 'black', fontSize: 22 }} />; | ||
const svgLocationIcon = ReactDOMServer.renderToString(locationIconJSX); | ||
|
||
return L.divIcon({ | ||
html: svgLocationIcon, | ||
iconSize: [22, 22], | ||
iconAnchor: [11, 11], | ||
popupAnchor: [0, -11], | ||
className: 'location-icon', | ||
}); | ||
}; | ||
|
||
onLocationFoundCallback: (coordinates, map) => | ||
map.flyTo([coordinates.coords.latitude, coordinates.coords.longitude], 15), | ||
const LocationControl = () => { | ||
const [userPosition, setUserPosition] = useState(null); | ||
const map = useMap(); | ||
|
||
onAdd: function (map) { | ||
const container = Leaflet.DomUtil.create('div', 'leaflet-bar leaflet-control'); | ||
const button = Leaflet.DomUtil.create('a', 'leaflet-control-button', container); | ||
const locationIcon = LocationIcon(); | ||
const handleLocationFound = e => { | ||
setUserPosition(e.latlng); | ||
map.flyTo(e.latlng, map.getZoom()); | ||
}; | ||
|
||
container.title = 'Twoja lokalizacja'; | ||
button.appendChild(locationIcon); | ||
map.once('locationfound', handleLocationFound); | ||
|
||
Leaflet.DomEvent.disableClickPropagation(button); | ||
Leaflet.DomEvent.on(button, 'click', () => { | ||
navigator.geolocation.getCurrentPosition(coordinates => | ||
this.onLocationFoundCallback(coordinates, map), | ||
); | ||
}); | ||
map.locate({ setView: false, maxZoom: 16, watch: true }); | ||
|
||
return container; | ||
}, | ||
if (!userPosition) { | ||
return null; | ||
} | ||
|
||
onRemove: function () {}, | ||
const { lat, lng } = userPosition; | ||
const radius = userPosition.accuracy / 2 || 0; | ||
|
||
return ( | ||
<> | ||
<LocationButton userPosition={userPosition} /> | ||
<CircleMarker center={[lat, lng]} radius={radius} /> | ||
<Marker position={userPosition} icon={createLocationIcon()} /> | ||
</> | ||
); | ||
}; | ||
|
||
export { LocationControl }; | ||
|
||
const positionType = PropTypes.shape({ | ||
lat: PropTypes.number.isRequired, | ||
lng: PropTypes.number.isRequired, | ||
}); | ||
|
||
LocationButton.propTypes = { | ||
userPosition: positionType.isRequired, | ||
}; |
10 changes: 0 additions & 10 deletions
10
src/components/Map/components/UserLocationMarker/UserLocationMarker.jsx
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import { MapContainer, TileLayer } from 'react-leaflet'; | ||
import MarkerClusterGroup from 'react-leaflet-cluster'; | ||
|
||
import { LocationControl } from './components/LocationControl/LocationControl'; | ||
import { mapConfig } from './map.config'; | ||
|
||
export const MapComponent = ({ markers }) => ( | ||
<MapContainer | ||
center={mapConfig.initialMapCoordinates} | ||
zoom={mapConfig.initialMapZoom} | ||
scrollWheelZoom | ||
style={{ height: '100%' }} | ||
> | ||
<TileLayer | ||
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" | ||
attribution='&copy <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors' | ||
maxZoom={mapConfig.maxMapZoom} | ||
/> | ||
|
||
<MarkerClusterGroup>{markers}</MarkerClusterGroup> | ||
|
||
<LocationControl /> | ||
</MapContainer> | ||
); | ||
|
||
MapComponent.propTypes = { | ||
markers: PropTypes.arrayOf( | ||
PropTypes.shape({ | ||
props: PropTypes.shape({ | ||
position: PropTypes.arrayOf(PropTypes.number), | ||
}), | ||
}), | ||
).isRequired, | ||
}; |
This file contains 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
Oops, something went wrong.