Spaces:
Runtime error
Runtime error
Commit
·
a93666a
1
Parent(s):
4b0d1a1
Add search bar for location, with dynamic marker and info box
Browse files- __pycache__/main.cpython-311.pyc +0 -0
- frontend/src/App.js +26 -3
- frontend/src/components/Map.js +37 -24
- main.py +1 -1
- requirements.txt +2 -1
__pycache__/main.cpython-311.pyc
CHANGED
Binary files a/__pycache__/main.cpython-311.pyc and b/__pycache__/main.cpython-311.pyc differ
|
|
frontend/src/App.js
CHANGED
@@ -1,20 +1,43 @@
|
|
1 |
// import logo from './logo.svg';
|
2 |
import './App.css';
|
3 |
import Map from './components/Map';
|
|
|
|
|
|
|
|
|
4 |
// import { MapContainer, TileLayer, useMapEvents } from 'react-leaflet';
|
5 |
|
6 |
|
7 |
function App() {
|
|
|
|
|
|
|
8 |
const handleMapClick = (lat, lng) => {
|
9 |
console.log(`Map clicked at latitude: ${lat}, longitude: ${lng}`);
|
10 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
return (
|
13 |
<div className="App">
|
14 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
</div>
|
16 |
);
|
17 |
-
|
18 |
}
|
19 |
|
20 |
|
|
|
1 |
// import logo from './logo.svg';
|
2 |
import './App.css';
|
3 |
import Map from './components/Map';
|
4 |
+
import React, { useState,
|
5 |
+
// useEffect,
|
6 |
+
// useCallback
|
7 |
+
} from 'react';
|
8 |
// import { MapContainer, TileLayer, useMapEvents } from 'react-leaflet';
|
9 |
|
10 |
|
11 |
function App() {
|
12 |
+
const [searchQuery, setSearchQuery] = useState('');
|
13 |
+
const [submittedQuery, setSubmittedQuery] = useState('');
|
14 |
+
|
15 |
const handleMapClick = (lat, lng) => {
|
16 |
console.log(`Map clicked at latitude: ${lat}, longitude: ${lng}`);
|
17 |
+
};
|
18 |
+
|
19 |
+
const handleSearch = (e) => {
|
20 |
+
e.preventDefault();
|
21 |
+
setSubmittedQuery(searchQuery);
|
22 |
+
console.log(`Search query: ${searchQuery}`);
|
23 |
+
};
|
24 |
|
25 |
return (
|
26 |
<div className="App">
|
27 |
+
<div className="search-container">
|
28 |
+
<form onSubmit={handleSearch}>
|
29 |
+
<input
|
30 |
+
type="text"
|
31 |
+
placeholder="Search for a location"
|
32 |
+
value={searchQuery}
|
33 |
+
onChange={(e) => setSearchQuery(e.target.value)}
|
34 |
+
/>
|
35 |
+
<button type="submit">Search</button>
|
36 |
+
</form>
|
37 |
+
</div>
|
38 |
+
<Map onMapClick={handleMapClick} searchQuery={submittedQuery} />
|
39 |
</div>
|
40 |
);
|
|
|
41 |
}
|
42 |
|
43 |
|
frontend/src/components/Map.js
CHANGED
@@ -1,4 +1,6 @@
|
|
1 |
-
import React, { useState, useEffect,
|
|
|
|
|
2 |
import { MapContainer, TileLayer,
|
3 |
useMapEvents,
|
4 |
Marker,
|
@@ -26,20 +28,35 @@ const ClickHandler = ({ onMapClick }) => {
|
|
26 |
return null;
|
27 |
};
|
28 |
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
const
|
32 |
const [wikiContent, setWikiContent] = useState(null);
|
33 |
const fetchWiki = async (pageName) => {
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
};
|
38 |
-
const markerPosition = [21.2514, 81.6296];
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
return (
|
41 |
<MapContainer
|
42 |
-
center={
|
43 |
zoom={2}
|
44 |
style={{ height: '100vh', width: '100%' }}
|
45 |
>
|
@@ -49,22 +66,18 @@ const Map = ( { onMapClick } ) => {
|
|
49 |
/>
|
50 |
|
51 |
<ClickHandler onMapClick={onMapClick}/>
|
52 |
-
<Marker position={markerPosition}
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
</Popup>
|
65 |
-
</Marker>
|
66 |
-
|
67 |
-
{/* Example marker */}
|
68 |
</MapContainer>
|
69 |
);
|
70 |
};
|
|
|
1 |
+
import React, { useState, useEffect,
|
2 |
+
// useCallback
|
3 |
+
} from 'react';
|
4 |
import { MapContainer, TileLayer,
|
5 |
useMapEvents,
|
6 |
Marker,
|
|
|
28 |
return null;
|
29 |
};
|
30 |
|
31 |
+
const BACKEND_URL = process.env.BACKEND_URL || 'http://localhost:8004';
|
32 |
+
console.log(BACKEND_URL);
|
33 |
|
34 |
+
const Map = ( { onMapClick, searchQuery } ) => {
|
35 |
+
const [markerPosition, setMarkerPosition] = useState([0,0]);
|
36 |
const [wikiContent, setWikiContent] = useState(null);
|
37 |
const fetchWiki = async (pageName) => {
|
38 |
+
try{
|
39 |
+
const res = await fetch(`${BACKEND_URL}/wiki/${pageName}`);
|
40 |
+
const data = await res.json();
|
41 |
+
setWikiContent(data);
|
42 |
+
if (data && data.latitude && data.longitude){
|
43 |
+
setMarkerPosition([data.latitude, data.longitude]);
|
44 |
+
}
|
45 |
+
}
|
46 |
+
catch (error) {
|
47 |
+
console.error("Error fetching Wikipedia content:", error);
|
48 |
+
}
|
49 |
};
|
50 |
+
// const markerPosition = [21.2514, 81.6296];
|
51 |
+
useEffect(() => {
|
52 |
+
if (searchQuery) {
|
53 |
+
fetchWiki(searchQuery);
|
54 |
+
}
|
55 |
+
}, [searchQuery]);
|
56 |
|
57 |
return (
|
58 |
<MapContainer
|
59 |
+
center={markerPosition}
|
60 |
zoom={2}
|
61 |
style={{ height: '100vh', width: '100%' }}
|
62 |
>
|
|
|
66 |
/>
|
67 |
|
68 |
<ClickHandler onMapClick={onMapClick}/>
|
69 |
+
<Marker position={markerPosition}>
|
70 |
+
<Popup minWidth={250}>
|
71 |
+
{wikiContent ? (
|
72 |
+
<>
|
73 |
+
<strong>{wikiContent.title}</strong><br />
|
74 |
+
<p style={{ fontSize: '12px' }}>{wikiContent.content}</p>
|
75 |
+
</>
|
76 |
+
) : (
|
77 |
+
"Search for a location to see information"
|
78 |
+
)}
|
79 |
+
</Popup>
|
80 |
+
</Marker>
|
|
|
|
|
|
|
|
|
81 |
</MapContainer>
|
82 |
);
|
83 |
};
|
main.py
CHANGED
@@ -12,7 +12,7 @@ loc = Nominatim(user_agent="GetLoc")
|
|
12 |
app.add_middleware(
|
13 |
CORSMiddleware,
|
14 |
allow_origins=["*"], # Replace with your frontend domain in prod
|
15 |
-
allow_credentials=
|
16 |
allow_methods=["*"],
|
17 |
allow_headers=["*"],
|
18 |
)
|
|
|
12 |
app.add_middleware(
|
13 |
CORSMiddleware,
|
14 |
allow_origins=["*"], # Replace with your frontend domain in prod
|
15 |
+
allow_credentials=False,
|
16 |
allow_methods=["*"],
|
17 |
allow_headers=["*"],
|
18 |
)
|
requirements.txt
CHANGED
@@ -7,4 +7,5 @@ passlib == 1.7.4
|
|
7 |
pyjwt == 2.9.0
|
8 |
pytest == 8.3.4
|
9 |
geopy == 2.4.1
|
10 |
-
gpxpy == 1.6.2
|
|
|
|
7 |
pyjwt == 2.9.0
|
8 |
pytest == 8.3.4
|
9 |
geopy == 2.4.1
|
10 |
+
gpxpy == 1.6.2
|
11 |
+
requests == 2.30.0
|