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

DV-000306: Add distance information. #368

Merged
merged 1 commit into from
Sep 3, 2024
Merged
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
7 changes: 7 additions & 0 deletions frontend/src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,13 @@ nav .navbar-collapse.show {
color: #777;
}

.distance-display {
padding: 0 0.3rem;
margin-top: 1.9rem;
text-align: left;
font-size: 1.25rem;
}

.logout-timer {
padding: 0 0.3rem;
margin-top: 1.9rem;
Expand Down
65 changes: 52 additions & 13 deletions frontend/src/components/MapView.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,39 @@ function MapView({getResult}) {
const [zoom] = useState(13);
const [address, setAddress] = useState('');
const [lineCoords, setLineCoords] = useState([]);
const [distance, setDistance] = useState(null);
const mapRef = useRef();

const getCurrentCoordinates = () => {
return new Promise((resolve, reject) => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
(position) => {
const {latitude, longitude} = position.coords;
resolve([latitude, longitude]);
},
(error) => {
console.error("Error getting location: ", error);
reject(error);
}
);
} else {
console.error("Geolocation is not supported by this browser.");
reject("Geolocation not supported");
}
});
};

useEffect(() => {
getCurrentCoordinates()
.then(coords => {
setCenter(coords);
})
.catch(error => {
console.error(error);
});
}, []);

useEffect(() => {
const regionName = parseRegionName(getResult);
if (!regionName) return;
Expand All @@ -38,8 +69,8 @@ function MapView({getResult}) {
setCenter(newCenter);
setAddress(regionName);

const distance = haversineDistance(initialCenter.current, newCenter);
console.log(`Distance: ${distance.toFixed(2)} km`);
const calculatedDistance = haversineDistance(initialCenter.current, newCenter);
setDistance(calculatedDistance.toFixed(2));

setLineCoords([initialCenter.current, newCenter]);
} catch (error) {
Expand Down Expand Up @@ -74,18 +105,26 @@ function MapView({getResult}) {
};

return (
<MapContainer ref={mapRef} center={center} zoom={zoom} scrollWheelZoom={false}>
<TileLayer url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"/>
<Marker position={center} icon={defaultIcon}>
<Popup>
{address || 'Loading address...'}
</Popup>
</Marker>
{lineCoords.length === 2 && (
<Polyline positions={lineCoords} color="blue"/>
<div>
<MapContainer ref={mapRef} center={center} zoom={zoom} scrollWheelZoom={false}>
<TileLayer url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"/>
<Marker position={center} icon={defaultIcon}>
<Popup>
{address || 'Loading address...'}
</Popup>
</Marker>
{lineCoords.length === 2 && (
<Polyline positions={lineCoords} color="blue"/>
)}
</MapContainer>
{}
{distance && (
<div className="distance-display">
Distance: {distance} km
</div>
)}
</MapContainer>
</div>
);
}

export default MapView;
export default MapView;
Loading