Skip to content

display current place on map #48

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

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions frontend/src/components/Map/BaseMapLayers.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// src/components/map/MapLayer.tsx
import { TileLayer, Marker } from "react-leaflet";

interface MapLayerProps {
initialPosition: [number, number];
}

const BaseMapLayers = ({ initialPosition }: MapLayerProps) => {
return (
<>
<TileLayer url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" />
<Marker position={initialPosition} />
</>
);
};

export default BaseMapLayers;
31 changes: 31 additions & 0 deletions frontend/src/components/Map/CurrentLocationMarker.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { useEffect, useRef } from "react";
import { useMap } from "react-leaflet";
import L from "leaflet";
import { useGeolocationContext } from "../../providers/GeolocationContext";

const CurrentLocationMarker = () => {
const map = useMap();
const { location, error } = useGeolocationContext();
const markerRef = useRef<L.Marker>();

useEffect(() => {
if (location) {
const lat = location.coords.latitude;
const lng = location.coords.longitude;

if (markerRef.current) {
markerRef.current.setLatLng([lat, lng]);
} else {
markerRef.current = L.marker([lat, lng]).addTo(map);
}
}
}, [location, map]);

if (error) {
console.error(`Geolocation error (${error.code}): ${error.message}`);
}

return null;
};

export default CurrentLocationMarker;
60 changes: 0 additions & 60 deletions frontend/src/components/Map/Map.tsx

This file was deleted.

38 changes: 38 additions & 0 deletions frontend/src/components/Map/MapContainerWrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// src/components/map/MapComponent.tsx
import React from "react";
import { MapContainer } from "react-leaflet";
import { useGeolocationContext } from "../../providers/GeolocationContext";
import ChangeView from "../../utils/ChangeView";
import CurrentLocationMarker from "./CurrentLocationMarker";
import BaseMapLayers from "./BaseMapLayers";
import { getMapZoom, getInitialCoords } from "./functions";
import "./style.css";

const MapContainerWrapper = () => {
const params = new URLSearchParams(window.location.search);
const paramLat = params.get("lat");
const paramLng = params.get("lng");

const { location } = useGeolocationContext();

const coords = getInitialCoords({
paramLat,
paramLng,
userLocation: location,
});
const zoom = getMapZoom(paramLat, paramLng);

return (
<MapContainer
center={coords}
zoom={zoom}
style={{ height: "100vh", width: "100%" }}
>
<BaseMapLayers initialPosition={coords} />
<ChangeView coords={coords} zoom={zoom} />
<CurrentLocationMarker />
</MapContainer>
);
};

export default MapContainerWrapper;
33 changes: 33 additions & 0 deletions frontend/src/components/Map/functions.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export const getMapZoom = (
paramLat: string | null,
paramLng: string | null
): number => {
if (paramLat === null && paramLng === null) {
return 20;
}
return 16;
};

interface InitialCoordsParams {
paramLat: string | null;
paramLng: string | null;
userLocation: GeolocationPosition | null;
}

export const getInitialCoords = ({
paramLat,
paramLng,
userLocation,
}: InitialCoordsParams): [number, number] => {
const defaultLat = 50.740717; // Coordonnées par défaut
const defaultLng = 2.258634;

const lat = paramLat
? parseFloat(paramLat)
: userLocation?.coords.latitude || defaultLat;
const lng = paramLng
? parseFloat(paramLng)
: userLocation?.coords.longitude || defaultLng;

return [lat, lng];
};
4 changes: 2 additions & 2 deletions frontend/src/components/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import MapComponent from "./Map/Map";
import MapContainerWrapper from "./Map/MapContainerWrapper";
import Navbar from "./NavBar/NavBar";
import Card from "./Card/Card";
import ListCards from "./ListCards/ListCards";

export { Navbar, Card, ListCards, MapComponent };
export { Navbar, Card, ListCards, MapContainerWrapper };
30 changes: 15 additions & 15 deletions frontend/src/hooks/useGeolocation.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { useEffect, useState } from "react";

const useGeolocation = () => {
interface LocationPosition {
coords: {
latitude: number;
longitude: number;
accuracy: number;
};
}
// interface LocationPosition {
// coords: {
// latitude: number;
// longitude: number;
// accuracy: number;
// };
// }

interface LocationPositionError {
code: number;
message: string;
}
// interface LocationPositionError {
// code: number;
// message: string;
// }

const [location, setLocation] = useState<LocationPosition | null>(null);
const [error, setError] = useState<LocationPositionError | null>(null);
const [location, setLocation] = useState<GeolocationPosition | null>(null);
const [error, setError] = useState<GeolocationPositionError | null>(null);

useEffect(() => {
const watchPosition = navigator.geolocation.watchPosition(
(position: LocationPosition) => {
(position: GeolocationPosition) => {
setLocation(position);
console.log("Geolocation position:", position);
},
(error: LocationPositionError) => {
(error: GeolocationPositionError) => {
setError(error);
console.error(`Geolocation error (${error.code}): ${error.message}`);
}
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/pages/Homepage/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from "react";
import { MapComponent } from "src/components";
import { MapContainerWrapper } from "src/components";

const HomePage = () => {
return (
<div>
<MapComponent />
<MapContainerWrapper />
</div>
);
};
Expand Down
19 changes: 19 additions & 0 deletions frontend/src/utils/ChangeView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useEffect } from "react";
import { useMap } from "react-leaflet";

interface ChangeViewProps {
coords: [number, number];
zoom: number;
}

const ChangeView = ({ coords, zoom }: ChangeViewProps) => {
const map = useMap();

useEffect(() => {
map.setView(coords, zoom);
}, [coords, zoom, map]);

return null;
};

export default ChangeView;