169 lines
4.3 KiB
Vue
169 lines
4.3 KiB
Vue
<template>
|
|
<div class="map-location-picker">
|
|
<div class="mb-2 d-flex justify-content-between align-items-center">
|
|
<p class="text-muted small m-0">
|
|
{{ __('Click on the map to place or move the pickup point.') }}
|
|
</p>
|
|
<span
|
|
v-if="lat && lng"
|
|
class="badge bg-light text-dark border"
|
|
>
|
|
Lat: {{ lat.toFixed(5) }} | Lng: {{ lng.toFixed(5) }}
|
|
</span>
|
|
</div>
|
|
|
|
<div
|
|
ref="mapContainer"
|
|
class="map-container rounded border"
|
|
/>
|
|
|
|
<input
|
|
type="hidden"
|
|
:name="latName"
|
|
:value="lat"
|
|
>
|
|
<input
|
|
type="hidden"
|
|
:name="lngName"
|
|
:value="lng"
|
|
>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import "leaflet/dist/leaflet.css";
|
|
import {
|
|
icon, LatLng, map as leafMap, marker as leafMarker, Marker, tileLayer
|
|
} from "leaflet";
|
|
import markerIcon2x from "leaflet/dist/images/marker-icon-2x.png";
|
|
import markerIcon from "leaflet/dist/images/marker-icon.png";
|
|
import markerShadow from "leaflet/dist/images/marker-shadow.png";
|
|
import {
|
|
onBeforeMount, onMounted, onUnmounted, ref, shallowRef, useAttrs
|
|
} from "vue";
|
|
|
|
import { __ } from "@modules/trans";
|
|
|
|
defineOptions({ name: "MapLocationPicker" });
|
|
|
|
const attrs = useAttrs();
|
|
const mapContainer = ref<HTMLElement | null>(null);
|
|
|
|
const map = shallowRef<L.Map | null>(null);
|
|
const marker = shallowRef<L.Marker | null>(null);
|
|
|
|
const latName = ref<string>("latitude");
|
|
const lngName = ref<string>("longitude");
|
|
const lat = ref<number | null>(null);
|
|
const lng = ref<number | null>(null);
|
|
|
|
|
|
|
|
const defaultIcon = icon({
|
|
iconUrl: markerIcon,
|
|
iconRetinaUrl: markerIcon2x,
|
|
shadowUrl: markerShadow,
|
|
iconSize: [25, 41],
|
|
iconAnchor: [12, 41],
|
|
popupAnchor: [1, -34],
|
|
shadowSize: [41, 41]
|
|
});
|
|
|
|
Marker.prototype.options.icon = defaultIcon;
|
|
|
|
onBeforeMount(() => {
|
|
if (!attrs.json) {
|
|
throw new Error("this component requires json");
|
|
}
|
|
});
|
|
|
|
// --- NOUVEAUTÉ : Fonction qui gère l'événement reçu depuis l'autocomplétion ---
|
|
const handleAddressSelected = (e: Event) => {
|
|
const customEvent = e as CustomEvent<{ lat: number; lng: number }>;
|
|
const newLat = customEvent.detail.lat;
|
|
const newLng = customEvent.detail.lng;
|
|
|
|
// Mise à jour des variables réactives (inputs cachés)
|
|
lat.value = newLat;
|
|
lng.value = newLng;
|
|
|
|
if (map.value) {
|
|
|
|
const newLatLng = new LatLng(newLat, newLng);
|
|
|
|
// Recentrer la carte (le '16' est le niveau de zoom pour être assez proche)
|
|
map.value.setView(newLatLng, 16);
|
|
|
|
// Mettre à jour le marqueur visuel
|
|
if (marker.value) {
|
|
marker.value.setLatLng(newLatLng);
|
|
} else {
|
|
|
|
marker.value = leafMarker(newLatLng).addTo(map.value);
|
|
}
|
|
}
|
|
};
|
|
|
|
onMounted(() => {
|
|
const json = String(attrs.json ?? "{}");
|
|
const data = JSON.parse(json);
|
|
|
|
latName.value = data.latName ?? "latitude";
|
|
lngName.value = data.lngName ?? "longitude";
|
|
lat.value = data.lat ? Number.parseFloat(data.lat) : null;
|
|
lng.value = data.lng ? Number.parseFloat(data.lng) : null;
|
|
|
|
initMap();
|
|
|
|
// On écoute l'événement dispatché par Blade
|
|
document.addEventListener("ban-address-selected", handleAddressSelected);
|
|
});
|
|
|
|
onUnmounted(() => {
|
|
// Toujours nettoyer les événements globaux
|
|
document.removeEventListener("ban-address-selected", handleAddressSelected);
|
|
});
|
|
|
|
const initMap = () => {
|
|
if (!mapContainer.value) return;
|
|
|
|
const defaultCenter: [number, number] = [46.2276, 2.2137];
|
|
const defaultZoom = lat.value && lng.value ? 15 : 5;
|
|
const center: [number, number] = lat.value && lng.value ? [lat.value, lng.value] : defaultCenter;
|
|
|
|
|
|
map.value = leafMap(mapContainer.value).setView(center, defaultZoom);
|
|
|
|
|
|
tileLayer(
|
|
"https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
|
|
{attribution: "© <a href=\"https://www.openstreetmap.org/copyright\">OpenStreetMap</a> contributors",}
|
|
).addTo(map.value);
|
|
|
|
if (lat.value && lng.value) {
|
|
|
|
marker.value = leafMarker([lat.value, lng.value]).addTo(map.value);
|
|
}
|
|
|
|
map.value.on("click", (e: L.LeafletMouseEvent) => {
|
|
lat.value = e.latlng.lat;
|
|
lng.value = e.latlng.lng;
|
|
|
|
if (marker.value) {
|
|
marker.value.setLatLng(e.latlng);
|
|
} else {
|
|
marker.value = leafMarker(e.latlng).addTo(map.value!);
|
|
}
|
|
});
|
|
};
|
|
</script>
|
|
|
|
<style lang="scss" scoped>
|
|
.map-location-picker {
|
|
.map-container {
|
|
height: 400px;
|
|
width: 100%;
|
|
z-index: 1;
|
|
}
|
|
}
|
|
</style>
|