Skip to content

Commit

Permalink
Merge pull request #307 from Bionic1251/main
Browse files Browse the repository at this point in the history
supporting user location
  • Loading branch information
Bionic1251 authored Nov 30, 2023
2 parents b5985af + 530a53a commit c0aec39
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions recommender-front/src/components/buttons/LocateButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import MyLocationIcon from '@mui/icons-material/MyLocation';
import { useMap } from 'react-leaflet';

function LocateButton({ handleSetOrigin }) {
const staticLat = 60.204178;
const staticLon = 24.961690;
const staticLat = 60.198805;
const staticLon = 24.935671;
const [locating, setLocating] = useState(false);

const buttonStyle = {
Expand All @@ -20,12 +20,29 @@ function LocateButton({ handleSetOrigin }) {

const map = useMap();

const success = (position) => {
console.log(`Latitude: ${position.coords.latitude}, Longitude: ${position.coords.longitude}`);
map.flyTo([position.coords.latitude, position.coords.longitude], map.getZoom());
handleSetOrigin(position.coords.latitude, position.coords.longitude);
setLocating(false);
};

const error = () => {
console.log('Unable to retrieve your location');
map.flyTo([staticLat, staticLon], map.getZoom());
handleSetOrigin(staticLat, staticLon);
setLocating(false);
};

const handleClick = () => {
if (!locating) {
setLocating(true);
map.flyTo([staticLat, staticLon], map.getZoom());
handleSetOrigin(staticLat, staticLon);
setLocating(false);

if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error);
} else {
console.log('Geolocation not supported');
}
}
};

Expand Down

0 comments on commit c0aec39

Please sign in to comment.