diff --git a/src/App.js b/src/App.js index 0b82b94..5529f93 100644 --- a/src/App.js +++ b/src/App.js @@ -55,7 +55,7 @@ function App() { Taver

- + } /> @@ -77,7 +77,7 @@ function App() { spacing={2}> - + } /> diff --git a/src/BaseInput.js b/src/BaseInput.js index bf8c529..6d5ca5f 100644 --- a/src/BaseInput.js +++ b/src/BaseInput.js @@ -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) { @@ -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 = []; diff --git a/src/currentLocation.js b/src/currentLocation.js deleted file mode 100644 index be8639d..0000000 --- a/src/currentLocation.js +++ /dev/null @@ -1,132 +0,0 @@ -import React from 'react'; -import ReactDOM from 'react-dom'; - -const mapStyles = { - map: { - position: 'absolute', - width: '100%', - height: '100%' - } -}; - -export class CurrentLocation extends React.Component { - - constructor(props) { - super(props); - - const { lat, lng } = this.props.initialCenter; - - this.state = { - currentLocation: { - lat: lat, - lng: lng - } - }; - } - - - componentDidUpdate(prevProps, prevState) { - if (prevProps.google !== this.props.google) { - this.loadMap(); - } - if (prevState.currentLocation !== this.state.currentLocation) { - this.recenterMap(); - } - } - recenterMap() { - const map = this.map; - const current = this.state.currentLocation; - const google = this.props.google; - const maps = google.maps; - - if (map) { - let center = new maps.LatLng(current.lat, current.lng); - map.panTo(center); - } - } - - componentDidMount() { - if (this.props.centerAroundCurrentLocation) { - if (navigator && navigator.geolocation) { - navigator.geolocation.getCurrentPosition(pos => { - const coords = pos.coords; - this.setState({ - currentLocation: { - lat: coords.latitude, - lng: coords.longitude - } - }); - }); - } - } - this.loadMap(); - } - loadMap() { - if (this.props && this.props.google) { - // checks if google is available - const { google } = this.props; - const maps = google.maps; - - const mapRef = this.refs.map; - - // reference to the actual DOM element - const node = ReactDOM.findDOMNode(mapRef); - - let { zoom } = this.props; - const { lat, lng } = this.state.currentLocation; - const center = new maps.LatLng(lat, lng); - - const mapConfig = Object.assign( - {}, - { - center: center, - zoom: zoom - } - ); - - // maps.Map() is constructor that instantiates the map - this.map = new maps.Map(node, mapConfig); - } - } - renderChildren() { - const { children } = this.props; - - if (!children) return; - - return React.Children.map(children, c => { - if (!c) return; - - return React.cloneElement(c, { - map: this.map, - google: this.props.google, - mapCenter: this.state.currentLocation - }); - }); - } - - - render() { - const style = Object.assign({}, mapStyles.map); - - return ( -

-
- Loading map... -
- {this.renderChildren()} -
- ); - } -} - -export default CurrentLocation; - -CurrentLocation.defaultProps = { - zoom: 14, - initialCenter: { - lat: -1.2884, - lng: 36.8233 - }, - centerAroundCurrentLocation: false, - visible: true -}; \ No newline at end of file