click and show detail as a pop-up

This commit is contained in:
matsuri 2023-08-24 23:18:05 +07:00
parent 4f3d74df6c
commit d2ed7fc3ea
2 changed files with 70 additions and 4 deletions

50
src/components/Popup.vue Normal file
View file

@ -0,0 +1,50 @@
<template>
<div v-if="showPopup" class="popup">
<div class="popup-content">
<p>{{ nearestStructureData.display_name }}</p>
<button @click="closePopup">Close</button>
</div>
</div>
</template>
<script>
export default {
props: {
nearestStructureData: Object,
onClose: Function,
},
data() {
return {
showPopup: true,
};
},
methods: {
closePopup() {
this.showPopup = false;
this.onClose();
},
},
};
</script>
<style scoped>
.popup {
position: fixed;
top: 50%; /* Set the popup to be vertically centered */
left: 50%; /* Set the popup to be horizontally centered */
transform: translate(-50%, -50%); /* Center the popup accurately */
background-color: white;
border: 1px solid #ccc;
padding: 10px;
box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.1);
z-index: 1000;
}
.popup-content {
margin: 0;
}
button {
margin-top: 10px;
}
</style>

View file

@ -1,4 +1,13 @@
<template> <template>
<div id="app">
<router-view />
<Popup
v-if="popupData"
:nearestStructureData="popupData"
:onClose="closePopup"
/>
</div>
<searchbar/> <searchbar/>
@ -33,6 +42,7 @@
<script setup> <script setup>
import searchbar from '@/components/searchbar.vue'; import searchbar from '@/components/searchbar.vue';
import Popup from "@/components/Popup.vue"; // Import the Popup componen
import { ref } from "vue"; import { ref } from "vue";
import axios from "axios"; import axios from "axios";
@ -41,23 +51,29 @@ const projection = ref("EPSG:4326");
const zoom = ref(19); const zoom = ref(19);
const rotation = ref(0); const rotation = ref(0);
const popupData = ref(null);
//Show API
const handleMapClick = async event => { const handleMapClick = async event => {
const clickedCoordinate = event.coordinate; const clickedCoordinate = event.coordinate;
console.log("Clicked coordinate:", clickedCoordinate);
try { try {
const response = await axios.get( const response = await axios.get(
`https://nominatim.openstreetmap.org/reverse?format=json&lat=${clickedCoordinate[1]}&lon=${clickedCoordinate[0]}` `https://nominatim.openstreetmap.org/reverse?format=json&lat=${clickedCoordinate[1]}&lon=${clickedCoordinate[0]}`
); );
console.log("API response:", response.data);
const nearestStructureData = response.data; const nearestStructureData = response.data;
console.log("Nearest structure:", nearestStructureData); popupData.value = nearestStructureData; // Show popup
} catch (error) { } catch (error) {
console.error("Error fetching reverse geocoding data:", error); console.error("Error fetching reverse geocoding data:", error);
} }
}; };
const closePopup = () => {
popupData.value = null; // Hide popup
};
</script> </script>
<style> <style>