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

feat: Suggesting best routes #40

Open
wants to merge 9 commits into
base: main
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*
yarn.lock
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sumana2001, should we keep this file in gitignore or not?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's ok to keep


.env
package-lock.json
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@emotion/react": "^11.9.3",
"@emotion/styled": "^11.9.3",
"@mui/icons-material": "^5.8.4",
"@mui/material": "^5.9.2",
"@react-leaflet/core": ">=1.0.0 <1.1.0 || ^1.1.1",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"dotenv": "^16.0.1",
"leaflet": "^1.8.0",
"leaflet-routing-machine": "^3.2.12",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-geocode": "^0.2.3",
"react-leaflet": "^4.0.1",
"react-scripts": "4.0.3",
"react-search-autocomplete": "^7.2.2",
Expand Down
23 changes: 23 additions & 0 deletions src/screen/routing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import leaflet from "leaflet";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This entire component could be shifted to /Components since it does not return a webpage/JSX.

import { createControlComponent } from "@react-leaflet/core";
import "leaflet-routing-machine";

function createRoutineMachineLayer({geocodes}) {
console.log(geocodes);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove console.log if you are done with testing

const instance = leaflet.Routing.control({
waypoints: [
leaflet.latLng(geocodes[0].lat, geocodes[0].lon),
leaflet.latLng(geocodes[1].lat, geocodes[1].lon)
],
lineOptions: {
styles: [{ color: "#6FA1EC", weight: 6 }]
},
show: false,
});

return instance;
};

const RoutingMachine = createControlComponent(createRoutineMachineLayer);

export default RoutingMachine;
32 changes: 32 additions & 0 deletions src/screen/trip.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.search-bar{
margin-top: 2rem;
margin-bottom: 4rem;
width: 50%;
margin-left: auto;
margin-right: auto;
display: flex;
flex-direction: row;
}

.leaflet-container {
width: 80%;
height: 75vh;
margin: 0 auto;
}

.route_button{
width: 150px;
padding: 10px;
background-color: blue;
color: white;
font-size: 20px;
margin-bottom: 10px;
border-radius: 15%;
border: none;
cursor: pointer;
}

.icon{
margin-top: 10px;
width: 50px;
}
83 changes: 78 additions & 5 deletions src/screen/trip.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,91 @@
import React from "react";
import React, {useState} from "react";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can import useEffect here so you don't have to write React.useEffect() everytime.

Also, React is already imported at the global scope for all /src files for React v16+ (I could be technically wrong here, what I mean is you don' need to import React explicitly in /src/ files.

import { Fab } from "../Components/common/Fab";
import { Food } from "../Components/Food";
import { Food }from "../Components/Food";
import './trip.css';
import { MapContainer, TileLayer, Marker, Popup } from 'react-leaflet';
import Search from "../Search";
import RoutingMachine from "./routing";
import ArrowRightAltIcon from '@mui/icons-material/ArrowRightAlt';


const navigateToHome = () => (window.location.href = "/");

export function Trip() {
const [currentCity, setCurrentCity] = useState(null);
const [fromCity, setFromCity] = useState(null);
const [toCity, setToCity] = useState(null);
const [cityList, setCityList] = useState([]);
const [isLoaded, setLoaded] = useState(false);

function setRoute() {
if(fromCity==null || toCity==null) {
alert("Something went wrong, try again!");
return;
}
fetch(`https://geocode.search.hereapi.com/v1/geocode?q=${fromCity}&apiKey=${process.env.REACT_APP_HEREAPI}`)
.then((res) => res.json())
.then((result) => {
//console.log(result);
let list = [...cityList];
list.push({
lat: result.items[0].position.lat,
lon: result.items[0].position.lng,
});
setCityList(list);
});
fetch(`https://geocode.search.hereapi.com/v1/geocode?q=${toCity}&apiKey=${process.env.REACT_APP_HEREAPI}`)
.then((res) => res.json())
.then((result) => {
//console.log(result);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this comment, also remove it from line 28

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two console logs on lines 15 and 39 of /Search.js. I know you have not made those changes. But if possible please remove those as well.

let list = [...cityList];
list.push({
lat: result.items[0].position.lat,
lon: result.items[0].position.lng,
});
setCityList(list);
setLoaded(true);
});

}


React.useEffect(() => {
document.title = "Plan a trip";
}, []);


window.navigator.geolocation.getCurrentPosition((position) => {
fetch(`https://api.openweathermap.org/data/2.5/weather?lat=${position.coords.latitude}&lon=${position.coords.longitude}&appid=${process.env.REACT_APP_APIKEY}`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar code exists in /screen/home.js. You can receive coordinates and city as props and save this API call.

CC: @Krishi-02

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: you are using lon to denote longitude whereas the rest of the app is using lng.

If you can replace your lon with lng, that would be great. Otherwise, don't forget to change the name of that key.

.then(res => res.json())
.then(result => {
//console.log(result)
setCurrentCity({
lat: result.coord.lat,
lon: result.coord.lon,
name: result.name
});
})
})

return (
}, []);
return (
<>
<div className="search-bar">
<Search setCity={setFromCity} />
<ArrowRightAltIcon className="icon"/>
<Search setCity={setToCity} />
</div>
<button onClick={setRoute} className="route_button">Find Route</button>
{currentCity==null? <div>Loading...</div> :
<MapContainer center={[currentCity.lat,currentCity.lon]} zoom={13} scrollWheelZoom={false}>
<TileLayer
attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
{cityList.length === 2 && <RoutingMachine geocodes={cityList} />}
<Marker position={[currentCity.lat,currentCity.lon]}>
<Popup>You are here!</Popup>
</Marker>
</MapContainer>
}
<Food country="PK" />
<Fab onClick={navigateToHome} icon="thermostat">
Weather
Expand Down
Loading