Spaces:
Runtime error
Runtime error
Commit
·
6d4c624
1
Parent(s):
fbf7aed
Basic click event listener added
Browse files- frontend/src/App.js +8 -19
- frontend/src/components/Map.js +14 -5
frontend/src/App.js
CHANGED
@@ -1,33 +1,22 @@
|
|
1 |
// import logo from './logo.svg';
|
2 |
import './App.css';
|
3 |
import Map from './components/Map';
|
|
|
|
|
4 |
|
5 |
function App() {
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
// <img src={logo} className="App-logo" alt="logo" />
|
10 |
-
// <p>
|
11 |
-
// Edit <code>src/App.js</code> and save to reload.
|
12 |
-
// </p>
|
13 |
-
// <a
|
14 |
-
// className="App-link"
|
15 |
-
// href="https://reactjs.org"
|
16 |
-
// target="_blank"
|
17 |
-
// rel="noopener noreferrer"
|
18 |
-
// >
|
19 |
-
// Learn React
|
20 |
-
// </a>
|
21 |
-
// </header>
|
22 |
-
// </div>
|
23 |
-
// );
|
24 |
|
25 |
return (
|
26 |
<div className="App">
|
27 |
-
<Map />
|
28 |
</div>
|
29 |
);
|
30 |
|
31 |
}
|
32 |
|
|
|
|
|
33 |
export default App;
|
|
|
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 |
+
<Map onMapClick={handleMapClick}/>
|
15 |
</div>
|
16 |
);
|
17 |
|
18 |
}
|
19 |
|
20 |
+
|
21 |
+
|
22 |
export default App;
|
frontend/src/components/Map.js
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import React from 'react';
|
2 |
import { MapContainer, TileLayer,
|
|
|
3 |
// Marker,
|
4 |
// Popup
|
5 |
} from 'react-leaflet';
|
@@ -14,7 +15,17 @@ L.Icon.Default.mergeOptions({
|
|
14 |
shadowUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-shadow.png',
|
15 |
});
|
16 |
|
17 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
return (
|
19 |
<MapContainer
|
20 |
center={[0, 0]}
|
@@ -26,10 +37,8 @@ const Map = () => {
|
|
26 |
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
|
27 |
/>
|
28 |
|
29 |
-
<
|
30 |
-
|
31 |
-
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
|
32 |
-
/>
|
33 |
</MapContainer>
|
34 |
);
|
35 |
};
|
|
|
1 |
import React from 'react';
|
2 |
import { MapContainer, TileLayer,
|
3 |
+
useMapEvents,
|
4 |
// Marker,
|
5 |
// Popup
|
6 |
} from 'react-leaflet';
|
|
|
15 |
shadowUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-shadow.png',
|
16 |
});
|
17 |
|
18 |
+
const ClickHandler = ({ onMapClick }) => {
|
19 |
+
useMapEvents({
|
20 |
+
click(e) {
|
21 |
+
const { lat, lng } = e.latlng;
|
22 |
+
onMapClick(lat, lng);
|
23 |
+
},
|
24 |
+
});
|
25 |
+
return null;
|
26 |
+
};
|
27 |
+
|
28 |
+
const Map = ( { onMapClick } ) => {
|
29 |
return (
|
30 |
<MapContainer
|
31 |
center={[0, 0]}
|
|
|
37 |
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
|
38 |
/>
|
39 |
|
40 |
+
<ClickHandler onMapClick={onMapClick}/>
|
41 |
+
|
|
|
|
|
42 |
</MapContainer>
|
43 |
);
|
44 |
};
|