-
-
Notifications
You must be signed in to change notification settings - Fork 11
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
base: main
Are you sure you want to change the base?
Changes from all commits
3e5e1b9
fca2999
a6b3b01
f9cf08b
593d08e
089001d
114143b
876b09e
c06e155
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
yarn.lock | ||
|
||
.env | ||
package-lock.json |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import leaflet from "leaflet"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This entire component could be shifted to |
||
import { createControlComponent } from "@react-leaflet/core"; | ||
import "leaflet-routing-machine"; | ||
|
||
function createRoutineMachineLayer({geocodes}) { | ||
console.log(geocodes); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,91 @@ | ||
import React from "react"; | ||
import React, {useState} from "react"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can import Also, React is already imported at the global scope for all |
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove this comment, also remove it from line 28 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are two console logs on lines 15 and 39 of |
||
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}`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar code exists in CC: @Krishi-02 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI: you are using If you can replace your |
||
.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='© <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 | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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