Skip to content

Commit

Permalink
Optimized for BOTH TIME AND LOCATION
Browse files Browse the repository at this point in the history
  • Loading branch information
sam9116 committed Feb 4, 2024
1 parent 5e27c08 commit 9aec7eb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 137 deletions.
4 changes: 2 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function App() {
<img src={window.location.origin + '/Taver.png'} alt="Taver" />
<PickDate updateStartDateInParent={setStartDate} updateEndDateInParent={setEndDate} />
<p/>
<BaseInput setConcerts={setConcerts} setUserLocation={setUserLocation} setMapStyle={setMapStyle} startDate={startDate} endDate={endDate} ref={childRef} />
<BaseInput setConcerts={setConcerts} setUserLocation={setUserLocation} setMapStyle={setMapStyle} startDate={startDate} endDate={endDate} concerts={concerts} userLocation={userLocation} ref={childRef} />
<Router>
<Routes>
<Route path="/" element={<AuthorizeSpotify />} />
Expand All @@ -77,7 +77,7 @@ function App() {
spacing={2}>
<Stack item xs={10} md={10} sx={{ display: { xs: 'flex', md: 'flex' } }}>
<PickDate updateStartDateInParent={setStartDate} updateEndDateInParent={setEndDate} />
<BaseInput setConcerts={setConcerts} setUserLocation={setUserLocation} setMapStyle={setMapStyle} startDate={startDate} endDate={endDate} ref={childRef} />
<BaseInput setConcerts={setConcerts} setUserLocation={setUserLocation} setMapStyle={setMapStyle} startDate={startDate} endDate={endDate} concerts={concerts} userLocation={userLocation} ref={childRef} />
<Router>
<Routes>
<Route path="/" element={<AuthorizeSpotify />} />
Expand Down
48 changes: 45 additions & 3 deletions src/BaseInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,28 @@ const mapStyles = [
];


// Convert degrees to radians
function degreesToRadians(degrees) {
return degrees * Math.PI / 180;
}

// Calculate the distance in kilometers between two coordinates
function distanceInKmBetweenEarthCoordinates(lat1, lon1, lat2, lon2) {
var earthRadiusKm = 6371;

const BaseInput = forwardRef(({ setConcerts, setUserLocation, setMapStyle, startDate, endDate }, ref) => {
var dLat = degreesToRadians(lat2 - lat1);
var dLon = degreesToRadians(lon2 - lon1);

lat1 = degreesToRadians(lat1);
lat2 = degreesToRadians(lat2);

var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return earthRadiusKm * c;
}

const BaseInput = forwardRef(({ setConcerts, setUserLocation, setMapStyle, startDate, endDate, concerts, userLocation }, ref) => {

useEffect(() => {
function showPosition(position) {
Expand Down Expand Up @@ -55,9 +74,32 @@ const BaseInput = forwardRef(({ setConcerts, setUserLocation, setMapStyle, start
if (resJson.length < 1) {
return;
}

resJson = resJson.sort((a, b) => {
return new Date(a.date) - new Date(b.date);
//get the last concert location in current list of concerts, if no concerts, use home location
var originPoint;

if (concerts.length > 0) {
//originPoint = new google.maps.LatLng(concerts[concerts.length - 1].location.latitude, concerts[concerts.length - 1].location.longitude);
originPoint = {
latitude: concerts[concerts.length - 1].location.latitude,
longitude: concerts[concerts.length - 1].location.longitude,
};
}
else {
//originPoint = new google.maps.LatLng(userLocation.coords.latitude, userLocation.coords.longitude);
originPoint = {
latitude: userLocation.coords.latitude,
longitude: userLocation.coords.longitude,
};
}

// Calculate the distance between the points
var distancea = distanceInKmBetweenEarthCoordinates(originPoint.latitude, originPoint.longitude, a.location.latitude, a.location.longitude);
var distanceb = distanceInKmBetweenEarthCoordinates(originPoint.latitude, originPoint.longitude, b.location.latitude, b.location.longitude);
console.log(`distancea: ${distancea}`);
console.log(`distanceb: ${distanceb}`);
return (new Date(a.date) - new Date(b.date)) && (distancea - distanceb);
});

let result = [];
Expand Down
132 changes: 0 additions & 132 deletions src/currentLocation.js

This file was deleted.

0 comments on commit 9aec7eb

Please sign in to comment.